Thread: Simple parse text file question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    30

    Simple parse text file question

    I am using this code to accept a filename of a text file input from the user which will parsed line by line. It takes a line of text and assigns it to an array element "line", and goes through the entire text file until there are no more lines.

    The problem I have with it is it does not function for text files with a certain number of lines. It seems to work fine for smaller text files, files with a small number of lines (something like 20-50 maybe), but does not work if there are several hundred lines. Why would this be, and how would I fix it?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line[1000];
      int i = 0;
      char filename[50];
    
      cout << "filename equals: ";
      cin >> filename;
      ifstream myfile (filename);
    
      if (myfile.is_open())
      {
        while (myfile.good())
        {
          getline (myfile,line[i]);
          i++;
        }
        myfile.close();
      }
      else cout << "Unable to open file\n";
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your while loop should also check the subscript for your array, like so
    Code:
        while (myfile.good() && i < 1000)
        {
          getline (myfile,line[i]);
          i++;
        }
    Or better
    Code:
    while ( i < 1000 && getline (myfile,line[i]) ) {
      i++;
    }
    To overcome this limitation, start with say
    std::vector<string> line;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    That did not change the behavior of the program given the input text files I have been using. For some reason, with these particular input files, certain ones still will not open successfully, even with that code change (although I did not try the vector one because I do not know how to implement it yet, never read about vectors).

    I am not aware of any special characters that will make the text file be read improperly. Is there anything within the content of a text file that would prevent it from being read by this program? I created a new text file that had several hundred lines, which was made up of just letters, spaces, and newline characters, and it was read perfectly with the original code.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you're going to have to bite the bullet and be clear about what's going on. You say "will not open correctly". Does this mean they do not open at all? (If not, check filename spelling and permissions.) Do they open but you get no data at all? (I would be surprised at this.) Do you get the right "amount" of data, but it looks like garbage? (If so, check how your files are encoded and whether that matches the encoding you're using to read it in.) Do you get half the file that looks correct, but then it stops in the middle? If so, look at where it stops and see if there's something interesting there. (I can't think of anything that would make this happen, but I suppose it's a possibility.) And you can always be checking errno whenever an .is_open or .good call fails to see what actually happened.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    What I meant to articulate more clearly was that...when I attempt to run this program using certain specific text files as input, the else statement of my program runs and the "Unable to open file" message is the output. This is the same message I get when I type in an input file name that does not exist in the directory in which the program is looking.

    I guess permissions are somehow the root of my problem. If I cut and paste the text contained within a file that I cannot parse, and I paste it into a new text file and attempt to parse it, the program will run. So somehow, in the pool of text files that I already have stored on my computer, some of those text files are not able to be parsed using this program. I initially thought it was due to the length of those files (in terms of text lines). Then I thought it had something to do with the actual character contents of the text files. Neither of those appear to be the problem, and it seems now that permissions, which I know nothing about, are allowing some text files to be parsed while disallowing others.
    Last edited by nair; 01-01-2011 at 07:46 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nair View Post
    What I meant to articulate more clearly was that...when I attempt to run this program using certain specific text files as input, the else statement of my program runs and the "Unable to open file" message is the output. This is the same message I get when I type in an input file name that does not exist in the directory in which the program is looking.

    I guess permissions are somehow the root of my problem. If I cut and paste the text contained within a file that I cannot parse, and I paste it into a new text file and attempt to parse it, the program will run. So somehow, in the pool of text files that I already have stored on my computer, some of those text files are not able to be parsed using this program. I initially thought it was due to the length of those files (in terms of text lines). Then I thought it had something to do with the actual character contents of the text files. Neither of those appear to be the problem, and it seems now that permissions, which I know nothing about, are allowing some text files to be parsed while disallowing others.
    Looks like now is the time to learn about how your operating system works. Also: cin >> will not read past spaces, so if there are any spaces in your filenames that's going to die a horrible death as well. (You should use getline again, in that case.)

    Are you able to see the contents of the files yourself using less/edit/whatever?

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    Ahhh...I was trying to use text files with names that had spaces in them. That's the issue. Thank you for spotting that and informing me.

    Ya I can open up all of the text files and edit them.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The lesson here is to be as specific as possible with your error messages. Turn that last line into
    Code:
    else cout << "Unable to open file " << filename << endl;
    and this post would never have happened.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nair View Post
    Ahhh...I was trying to use text files with names that had spaces in them. That's the issue. Thank you for spotting that and informing me.

    Ya I can open up all of the text files and edit them.
    If you use std::getline instead of std::cin >>, you can get rid around this problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Replies: 15
    Last Post: 10-31-2005, 08:29 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. a simple file stream question
    By terracota in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2004, 12:03 AM