reading the next line in a text file

This is a discussion on reading the next line in a text file within the C Programming forums, part of the General Programming Boards category; Hello, I'm having trouble getting my file pointer to increment to the next line accurately.It reads the first line just ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question reading the next line in a text file

    Hello,
    I'm having trouble getting my file pointer to increment to the next line accurately.It reads the first line just fine,but when it loops thru the second time it reads the text from different areas.I feel like i'm close but,I don't really know.If someone could point me in the right direction.I know my code looks horrible and I really need to clean it up.I apologize.
    Scott

    int main()
    {
    FILE *inv;
    char search_key[6];
    int recordcount = 0;
    int fieldcount = 0;

    char between=TRUE;
    int chr;
    int a;
    char record[85];
    char item_num[7];
    char category[5];
    char ean[13];
    char codabar[6];
    char vendor_num[4];
    char ven_part_num[11];
    char title[40];
    banner();
    printf("* *\n");
    printf("* Lets view records by item# *\n");
    printf("****************************************** *************\n");
    printf(" Enter an Item# : ");scanf("%s", &search_key);


    do
    {

    if ( (inv = fopen("inventory.txt","rt")) == NULL )
    fprintf(stderr,"Could not open file inventory.txt\n");
    }
    while (inv == NULL);



    do
    {



    fgets(item_num,7,inv);
    fgets(category,4,inv);
    fgets(ean,14,inv);
    fgets(codabar,9,inv);
    fgets(vendor_num,4,inv);
    fgets(ven_part_num,12,inv);
    fgets(title,40,inv);

    }
    while(search_key != item_num);

  2. #2
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    It might be helpful for you to include a snippet of your input file. However, with that being said I might venture a guess at one of the following causes and things that you can try.

    One of your lines either contains more characters than you have specified in your fgets for the variable you are attempting to read in. Or perhaps one of your lines is missing the newline character.

    Also, what might prove very helpful to you for knowing where the problem is - check the return result of each of your fgets for null to know exactly which line is failing.

    -Just some ideas for things to look at, I'm sure others will have suggestions for you as well.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    Have you ever thought of using structures when saving records to a file?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    input file snippet

    Penny, here's a snippet of the input file.
    I tried changing the number of characters that fgets pulled for each and the way it is right now the firstline reads correctly.
    Scott


    9330 100 978158008328 9330 301 1580083285 WRITE RIGHT! A DESKTOP DIGEST OF PUNCTUA
    9331 100 978073570971 9331 104 0735709718 CISCO CCNA EXAM 640-507: CERTIFICATION G
    11499 100 978020528034X 11499 0 020528034X ESSENTIALS OF ELEMENTARY READING (P)
    11862 100 9780876296207 11862 146 0876296207 BUILDING CONSTRUCTION COST DATA (2002) (

  5. #5
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    Right As Xoid said Use structures And do binary input output with file.
    Use functions such as fread() and fwrite() to read or write complete structures in and out of a file.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    If I was going to create the inputfiles I would try binary.However I'm working with a flatfile database.So text is the rule.I was planning on cleaning up the code later and placing all the character strings in a structure for each input file.Right now I only have gthe one inputfile but there will be five text files.
    hmm? I could use a structure to read and write a complete record to the file. Is that only with a binary file?

    scott

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    got it! at least reading the subsequent lines.It doesn't seem to break out of the do-while loop though. Any thoughts?

    Thank you Penney for pointing me in the right direction.

    Scott

  8. #8
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    Yes, you can't use != to compare strings. Try the strcmp function and it returns 0 if both strings are the same.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    // ******************************************

    do
    {

    if ( (inv = fopen("inventory.txt","rt")) == NULL )
    fprintf(stderr,"Could not open file inventory.txt\n");
    }
    while (inv == NULL);

    // *******************************************

    To detect if the file exists you don't need to encase in a do...while loop, also you don't need to call fprintf(), just use printf.

    try this:

    if ((inv = fopen("inventory.txt"),"rt") != NULL)
    {
    // do something
    }
    else {
    printf("Error opening file");
    }

    just a thought, if I have completely lost the plot then please correct me, thanks! lol

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    also in your while loop check for EOF using feof just as an extra

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Ok it reads the input file correctly.I changed the while statement at the end of the input loop to
    while((strcmp(item_num,searchkey)) !=0);

    It still doesn't seem to break out of the loop.Is this invalid?ShouldI instead .....

    do
    {



    fgets(item_num,7,inv);
    fgets(category,4,inv);
    fgets(ean,14,inv);
    fgets(codabar,9,inv);
    fgets(vendor_num,4,inv);
    fgets(ven_part_num,12,inv);
    fgets(title,45,inv);
    a =strcmp(item_num,searchkey);
    }
    while(a != 0);

    would that be preferable.

    Scott

  12. #12
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    What you had is correct, however note that the strcmp is case sensitive and spaces matter as well. Your loop is set to loop as long as you dont find the key. the strings "abc " and "abc" are different and so is "Abc" and "abc" so not knowing what you have, you might want to trim the strings and convert to same case. Also you probably want to kick out of the loop if either you find the key or you reached the EOF unless you know that the key will always be there.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Maybe this will help:
    Code:
    #include <stdio.h>
    
    typedef struct Item
    {
      char  record[85];
      char  item_num[7];
      char  category[5];
      char  ean[13];
      char  codabar[6];
      char  vendor_num[4];
      char  ven_part_num[11];
      char  title[40];
    } Item_t;
    
    int main(void)
    {
      FILE  *fp;
      char  buf[BUFSIZ];
      Item_t item;
        
      if ((fp = fopen("junk1.txt", "r")) == NULL)
      {
        perror ("junk1.txt");
        return 0;
      }
      
      while (fgets(buf, sizeof buf, fp))
      {
        /* 
         * Now work with the line in here to add each bit 
         * of data to the corresponding variables within the
         * item struct.
         * If all data present, do the comparison against
         * the required item#, if matched: break loop.
         * Hint: use the space as the seperator for all fields
         */
      }
      
      fclose(fp);
      
      return(0);
    }
    fgets() is really for getting a complete line, not just a part of one, particularly when the "part" is variable in length.

    And everyone, please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-19-2009, 12:44 AM
  2. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 03:30 PM
  3. end of line in text file
    By spveer in forum C Programming
    Replies: 5
    Last Post: 08-18-2005, 01:43 AM
  4. Reading a line from a text file Please help
    By Blizzarddog in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 01:35 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21