Using fstream help.

This is a discussion on Using fstream help. within the C++ Programming forums, part of the General Programming Boards category; I need to read in up to six text files from command line agruments. This is the code I have ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Using fstream help.

    I need to read in up to six text files from command line agruments. This is the code I have but it exits at the if(!fin) exit(1); line and I don't know why.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       vector<vector<string> >   words;
       string word;
       int wordCount =0; 
       
       cout<<"before loop";
       for(int i=1; i < argc; i++)
       {
          ifstream fin(argv[i]);
          if(!fin) exit(1);
      
          cout<<"after loop";
          wordCount =0;
         
         
          while(!fin.eof())
          {
             fin>>word;
             cout<<word<< "-";
             words[i][wordCount] = word;
             wordCount++;
          }
    
          
       }
    
    for(int i =0; i< words.size(); i++)
       for(int x=0; x< words[i].size(); x++)
          cout<< words[i][x] << " ";
    
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Probably because the first file failed to open.

    Are you sure the path is correct? Use an absolute path just to make sure. Since it is an input file the file must exist at that location.

    BTW, while (!fin.eof()) often leads to incorrect data by processing the last word twice. The common way to do this is to move the read into the while:
    Code:
    while(fin >> word)

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    I'm doing over a server but I checked and the text file is in the same directory as the program. I saved the file as test.txt and to run it I do ./execProg test

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    But "test" is different that "text.txt". That's probably your problem. Your fstream doesn't do OS specific stuff like automatically add file extensions and stuff. You have to use the exact filename.

    If it still doesn't work, you should try to use an absolute path because the working directory is not always the same as the current directory. It sounds like it will be fine in your case, but if it isn't you should use the absolute path to make sure that's not the problem.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    the error i get is: segmentation fault (core dumped)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    That means it's opening the file. The problem is probably because you are accessing elements in words that don't exist. You created that vector of vectors as empty. When you use operator[] on a vector (just like an array) there has to be something at that location. You probably want to either push_back a new vector for every file that you open, or construct words with argc-1 inner vectors to start with.

    Once you fix that, you'll have the same issue with the inner vectors, although simply using push_back instead of wordcount will likely fix that.

    Note that if you use cerr instead of cout, or if you add endl or flush to your cout statements, the output should show how far you get before the segmentation fault. If you aren't seeing any output before that error, it's likely just because the two cout statements hadn't been flushed to the console yet.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Right now I have vector<vector<string> > words; but how do I define it with the first dimesion using argc-1 and not define the second dimesion so I can use .push_back() Thanks for your help.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Code:
    vector<vector<string> > words(5);
    That code creates a vector of 5 empty vectors. Adjust to suit your needs.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Got it. Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

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