Reading in a file

This is a discussion on Reading in a file within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read in a series of text files in c++ but running into a bit of trouble. I've ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Reading in a file

    I'm trying to read in a series of text files in c++ but running into a bit of trouble. I've never done this before and I'm trying to follow the guide here: http://www.cplusplus.com/doc/tutorial/files.html

    Here's my code:
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include "thread.h"
    
    using namespace std;
    
    int numThreads;
    
    int main(int argc,char* argv[])
    {
    	ifstream in;
    	string line;
    	cout << argv[1] << endl;
    	numThreads = atoi(argv[1]);
    	cout << "Number of threads: " << numThreads << endl;
    	for(int i=2;i<argc;i++)
    	{
    		cout <<"Opening:" << argv[i] << endl;	
     		in.open(argv[i]);	
     		while(!in.eof())
     		{
    			cout << "A LINE" << endl;
     			getline(in,line);
     			cout << line << endl;
     		}
     		in.close();
    	}	
    	return 0;
    }
    Just to explain what's happening...I'm passing in a series of command line arguments, the first of which is the number of arguments I'm passing, and the other ones are files. It works fine until I get to the while loop. It just keeps printing "A LINE" over and over and never prints anything from the file.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I get:
    Code:
    Opening:files.cpp
    A LINE
    #include <cstdlib>
    A LINE
    #include <fstream>
    A LINE
    #include <iostream>
    A LINE
    
    A LINE
    using namespace std;
    A LINE
    
    A LINE
    int numThreads;
    A LINE
    
    A LINE
    int main(int argc,char* argv[])
    A LINE
    {
    A LINE
        ifstream in;
    A LINE
        string line;
    A LINE
        cout << argv[1] << endl;
    A LINE
        numThreads = atoi(argv[1]);
    A LINE
        cout << "Number of threads: " << numThreads << endl;
    A LINE
        for(int i=2;i<argc;i++)
    A LINE
        {
    A LINE
    	cout <<"Opening:" << argv[i] << endl;	
    A LINE
    	in.open(argv[i]);	
    A LINE
    	while(!in.eof())
    A LINE
    	{
    A LINE
    	    cout << "A LINE" << endl;
    A LINE
    	    getline(in,line);
    A LINE
    	    cout << line << endl;
    A LINE
    	}
    A LINE
    	in.close();
    A LINE
        }	
    A LINE
        return 0;
    A LINE
    }
    A LINE
    
    A LINE
    I suppose you could check to make sure (1) you provide all the filenames you claim you do and (2) they can each be opened.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    It looks like you passed the file name that you are running as an argument to be opened?

    Anyways, it looks the files aren't being opened. I guess I never really considered where the files are. They are in my src code directory but I'm not really sure where they are supposed to go. I'm running KDevelop on Ubuntu but I don't think that helps any

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Figured it out. For anyone who cares, the home directory is under debug/src

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I had to use something. But the files need to be in the directory you are running from, or you need to specify the full path.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 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. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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