Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to read only integers from a file with c

how to read only integers from a file with c

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    reemamjd
    wrote on last edited by
    #1

    Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78

    F.R.Majeed

    V D 2 Replies Last reply
    0
    • R reemamjd

      Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78

      F.R.Majeed

      V Offline
      V Offline
      venomation
      wrote on last edited by
      #2

      This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]

      L R 2 Replies Last reply
      0
      • V venomation

        This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        venomation wrote:

        it is simply the case of casting the string to an int and visa versa.

        You cannot cast a string to an int, it must be converted.

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        1 Reply Last reply
        0
        • V venomation

          This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]

          R Offline
          R Offline
          reemamjd
          wrote on last edited by
          #4

          @ venomation :confused: what u mention above could be done through oop language but i want to know how to do it through C (sop)

          L 1 Reply Last reply
          0
          • R reemamjd

            @ venomation :confused: what u mention above could be done through oop language but i want to know how to do it through C (sop)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Doing this with C language programming is not difficult; just a few statements.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            1 Reply Last reply
            0
            • R reemamjd

              Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78

              F.R.Majeed

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Have you considered fscanf()?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              R 1 Reply Last reply
              0
              • D David Crow

                Have you considered fscanf()?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                R Offline
                R Offline
                reemamjd
                wrote on last edited by
                #7

                fp=fopen("SDetails.txt","r+");
                while(1)
                {
                ch=fgetc(fp);
                if(ch==EOF)
                break;
                fscanf(fp, "%d", &myInt);
                printf(" %d ",myInt);
                sum=sum+myInt;
                }

                printf("%d",sum);
                fclose(fp);

                what i did was like above ..but it print the same int 3 times ..i cant find the error...please help now i can read only ints from the file..thx

                D 1 Reply Last reply
                0
                • R reemamjd

                  fp=fopen("SDetails.txt","r+");
                  while(1)
                  {
                  ch=fgetc(fp);
                  if(ch==EOF)
                  break;
                  fscanf(fp, "%d", &myInt);
                  printf(" %d ",myInt);
                  sum=sum+myInt;
                  }

                  printf("%d",sum);
                  fclose(fp);

                  what i did was like above ..but it print the same int 3 times ..i cant find the error...please help now i can read only ints from the file..thx

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  What you have will never work as you expect. The call to fgetc() is going to advance the file pointer, then fscanf() is going to be reading from the middle of some byte. Have you checked the return value from fscanf()? If it does not find a number to read from the file, it may be retaining the last read value. Is your file laid out something like: Name1,288,7099,441 Name2,1500 Name3,587,208

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups