Thread: Keep reading lines in a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Keep reading lines in a file

    I need to basically make a loop where i read everyline in a file but how would i tell the loop to stop when it reaches the end of a file?

    while (empinfo <<my filename != \0)

    i tired that but that was useless

    Code:
          empinfo = fopen("employees.dat", "r");
          if (fp == NULL) {
             printf("Cannot open &#37;s\n", fp);
             }
          else {
               /*printf("What is the 4 digit employee number: ");
               scanf("%s", first);*/
               do {
               fscanf(empinfo, "%d:%[^:]:%[^:]:%d:%lf\n", &empnum, last, first, &emprate, &emphours);
               printf("%s ", first);
               k++;
               } while (k != 6);
               fclose(empinfo);
               }
    thats what im doing right now, but i need it to find out when to stop since ill be adding lines into the file so i cant go into the code manually everytime
    Last edited by bigmac(rexdale); 12-01-2007 at 11:05 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Check fscanf for EOF. Also remember that reading strings with scanf is unsafe unless you properly use format specifiers which you don't. Recommend you use fgets instead. Think about that.
    And fix your indentation!

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ya,i commented out that part because i didn't need it ya im rushing this so its not indented properly.

    Anyways ill try the EOF part.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IT doesn't matter if you rush or not - it's NOT an excuse for poor indenting.
    You indent as you type and NOT the other way around.
    Poor indenting creates more bugs than it solves so always indent when you first write your code, and not afterwards or as an afterthought.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok ill do it

    anyways, i used the feof and it works thanks alot really helped

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Beware that feof only returns true AFTER you've attempted to read beyond the file. What does this mean? If you read something from the file, beyond the the file, feof will be true, but then you do another read (because you use feof as a loop condition), the second read will fail miserably.
    See the FAQ for more info on that.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    so if i was to read the file again it will fail? What about if i used rewind?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    while (! feof(my_file) )
    {
    	fread(my_buf, my_size, 1, my_file);
    	fread(my_buf, my_size, 1, my_file);
    }
    The problem with this code is that if the first fread fails, then you may get unpredicted results with the second read, since you're expecting the code to continuously succeed until feof returns false at which time you abort.
    See the FAQ for more on this.
    (Rule: using feof as a condition for the loop is bad; consider using a read as a condition since it will fail if you try to read beyond the file.)

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok so i should modify it to this?
    Code:
      while (fgets(buf, sizeof(buf), fp) != NULL)
      {
        printf ("Line &#37;4d: %s", i, buf);
        i++;
      }
    also, i wanna pass a array into a function but when it goes into the fuction the array equals zero

    Code:
    #include <stdio.h>
       double cGross(double hours[], double emprate) { 
           printf("%d\n", hours[1]); <<displays zero
           if (emprate = 1){
                       emprate = emprate * hours[1];
                       }
           return emprate;
       }
       
       main() {
          FILE *fp;
          FILE *empinfo;
          int line[9], i = 0, k = 0;
          
          /*double empnum = 0;*/
          double emphours,  hours[9], gross = 0, emprate;
          char first[256], last[256], empnum[256];
          /*Loads the hourly rates in an array*/
          fp = fopen("payrates.dat", "r");
          if (fp == NULL)
             printf("Cannot open %s\n", fp);
             
          else {  
             for (i = 0; i < 10; i++) {   
               
             fscanf(fp, "%lf\n", &hours[i]);
             printf("%.2lf\n", hours[i]);
             }
             fclose(fp);
          }
          /*end of hourly rates*/
          
          
          empinfo = fopen("employees.dat", "r");
          if (fp == NULL) {
             printf("Cannot open %s\n", fp);
             }
          else {
               /*printf("What is the 4 digit employee number: ");
               scanf("%s", first);*/
               do {
                   
                   fscanf(empinfo, "%[^:]:%[^:]:%[^:]:%d:%lf\n", empnum, last, first, &emprate, &emphours);
                   gross = cGross(hours, emprate);
                   printf("%d ", gross);
                   printf("%s ", empnum);
                   printf("%s ", first);
                   printf("%s ", last);
                   printf("%5.d ", emprate);
                   printf("%5.2lf\n", emphours);
               } while (! feof(empinfo));
               fclose(empinfo);
               }
       }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The loop is fine.
    Why hours[1] is 0, I don't know. I don't have the file contents so I can't debug, but I don't see anything wrong with the code.

    And now for comments:

    Code:
    int main() {
    
    main should return int! Thought I told you...

    Code:
    int line[9]
    Never used anywhere so you can remove it.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ya there was a couple stuff i forgot to do so end of main i should put return 0;

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ya, you should do that. Compilers, nowadays...

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok fixed the stuff you suggested, but about the hours array the values are saved because when i do a loop of the hours array it displays its contents but when sent to the functions cGross it displays 0 for all, somethings is probably not passing

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Syntax is correct. I tried initializing the array with values and calling the function to display them later and it worked fine, so I would assume something else is happening, but I can't tested without the contents of the files.
    You don't use a debugger, do you?

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    if you have the time for it

    Code:
    7.25
    8.00
    10.00
    11.00
    12.00
    15.75
    25.00
    40.00
    85.00
    125.00

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 01:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM