Thread: .get Member Function and EOF???

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Angry .get Member Function and EOF???

    I have a set of log files that I need to read through line by line. I have multiple files that need to be read. Once one file is finished I need to close it and open the next and read it line by line and so on and so forth.

    I should know this, but for some reason I am over looking something and have been unable to see what.

    Does this work?

    Code:
    char line[15];
    
    // Other code.
    
    inFile.get(line,15);
    while (!inFile.eof())
    {
        // Do stuff.
        inFile.get(line,15);
    }
    Within the "// Do Stuff" I read in other characters and process them. My output file is correct, except that my program never gets to the end of the first input file. Right now the program is set to end after the first input file (once I get it to work right I'll make it read other files).

    Any help would be appreciated! I've been racking my brain on this for three hours now, and it's something that I should know.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you mean to use getline? If so, then that code might work. It will skip the last line if your file does not end in a newline, though.

    I'd suggest using strings instead of character arrays so you don't have to limit the input to 14 characters.

    Then I'd make the loop like this:
    Code:
    std::string line;
    while (getline(inFile, line))
    {
        // Do stuff.
    }
    BTW, if you use the same ifstream variable for each file, don't forget to call clear() after you finish with each file.
    Last edited by Daved; 05-11-2008 at 10:13 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    I was using getlines, but I had some issues with it. I'll give it a try.

    I'll stick with character arrays simply because I need to get 14 characters. I'm looking for a certain character at the 13th character of each line.

    I belive the file ends in a newline.

    I remember the last time I forget to use clear(). What a waste of time it was until I found that error!!

    Thanks for the help.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If all your lines are less than 15 characters, then a string will work as well as a character array (you can check the 13th character of a string the same way as a character array). And of course using the string class is much better in general.

    If any line is 15 characters or longer, then using the character array version will mean that you don't get the whole line in the read, and the next time through the loop you'll get the rest of the line. It also might set the failbit (I don't remember for sure), which would have to be cleared. That's extra work that doesn't need to be done if you don't restrict the size of the line (like if you used string).

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Each line is an arbitrary length. I read in the rest of the line in // Do Stuff.

    I use line as an array because I want a specific character and I only need to read that far into the line to see how to process the rest of the line.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok. I still might use strings, but I won't argue with your choice here.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    I didn't want to do this, but I have found where my code is hanging. I figured it would be easier for you guys to see what I mean. I have made a couple of small changes to the main loop.

    Code:
        ifstream inFile;  // Used to open the logs.
        ofstream outFile; // Used to make output.
    
        char start[7];  // Holds the start date (YYMMDD).
        char end[7];    // Holds the end date (YYMMDD).
        char curr[7];   // Holds the current date. (YYMMDD).
        char fName[13]; // Holds the file name to open.
        char line[15];  // Start of line.
        char name[256]; // For holding a name.
        char ch;        // Holds an idividual character from the file.
        int  test;      // TEST VARIABLE.
        int  check[2];  // For checking for values.
        int  type;      // For Message or PHONEMAIL formatting differences.
    
        strcpy(curr, "080310"); /* TEST CODE - NEEDS TO BE REMOVED LATER */
        strcpy(fName, "mr");   // Add 'mr' to the filename.
        strcat(fName, curr);   // Add the current date to the filename.
        strcat(fName, ".log"); // Add the extension to the filename.
    
        // Open the input and output file and do error checking.
        inFile.open(fName);
        outFile.open("LogReport.txt");
    
        inFile.get(ch);
        while (!inFile.eof())
        {
          // Get the beginning of a line and see how to process it.
          inFile.get(line, 14);
          type = 0;
          if (line[12] == 'M')
          {
            type = 1;
          }
          
          // Process as Message line.      
          if (type == 1)
          {
            // These first two while loops find the characters that delimit where 
            // a name is going to be located.
            check[0] = 0;
            while (!check[0])
            {
              inFile.get(ch);
              if (ch == '(')
              {
                check[0] = 1;
              }
              cout << "stuck?" << endl;
              cout << ch << endl;
            }
            check[1] = 0;
            while (!check[1])
            {
              inFile.get(ch);
              if (ch == '/')
              {
                check[1] = 1;
              }
            }
            inFile.get(name, 256, ')');   // Pickup the name.
            outFile << name << endl;      // Output the name.
            
            // Find the end of the current line.
            inFile.get(ch);
            while (ch != '\n')
            {
              inFile.get(ch);
            }
          }
          
          // Process as PHONEMAIL line
          else
          {
            // The current line is PHONEMAIL.
            cout << "PHONEMAIL - Process me!" << endl;
            return 0;
          }
          inFile.get(ch); // Get the next input.
          cout << ch << endl;
        }
    The bold loop is where my code gets stuck. I have poured over this and I can't seem to find what is wrong. I have a feeling that this is from something stupid that I am doing.

    Thanks for all the help!
    Last edited by dfghjk; 05-12-2008 at 09:02 AM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken, getline for C-style strings sets the fail bit if it reads less than up to the newline delimiter. Once it is set, you shouldn't be able to continue getting input, unless you clear the error state first.

    There is also a possibility that the file doesn't contain the characters that you are looking for beyond that point in the file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    I have made some changes to my program. I am now using a Flex file to parse my input and simply drop the names into a linked list for sorting and duplicate removal.

    Thanks for the ideas!

    I really need to learn more about using strings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. eof member function on the stream cin
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 05:00 PM