Input files not recognised

When I want files to be read in, they are not recognised.

Page 1 of 1

11 Replies - 617 Views - Last Post: 18 March 2010 - 02:46 PM Rate Topic: -----

#1 jayd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 10

Input files not recognised

Posted 18 March 2010 - 08:02 AM

Hi all,
I'm having a very frustrating time getting what should be a simple program to work. For now, I simply want .cpp and .h files to be recognised by being read in from another text file. My code is below.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

void newInput(const char* entry)
{
		cout << entry << endl;
		fstream currentFile (entry);
		if(!currentFile) 
		{
			cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
			exit(1);
  		}
  		else
  		{
  			cout << "File Input" << endl;
  		}
		currentFile.close();
}

int main(int argc, char *argv[])
{

	if (argc != 2)
    {
       cout << "Incorrect execution - please enter a text file (only one) next time you run the program. Program shall now exit.\n" << endl;
       return 0;
    }
	
	fstream myFile (argv[1]);
	string line;
	bool eofFound = false;

	if(!myFile) 
	{
	    cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
	    return 0;
  	}

	while (!myFile.eof())
	{
		//The file is read in line by line, until the end of file
		getline (myFile,line);

		//If it's not the EOF line, the file is input
		if (line != "EOF")
		{
			newInput(line.data());
		}
		//If the line is the EOF line, we know that it is the end of the file
		else if (line == "EOF")
		{
			eofFound = true;
		}
	}
	
	//If no 'EOF' is found, then an error is displayed and the program exits 
	if (!eofFound)
	{
		cout << "No EOF line in file - please check your input, and try again. Program shall now exit.\n";
		return 0;
	}
	//closes the file
	myFile.close();


	cout << "\nProgram finished." << endl;

	return EXIT_SUCCESS;
}




I provide a text file that simply has

Test.cpp
EOF

as the input file the text file is provided as the second argument when the program is executed. I've attatched it with the post.

My problem is that where I want the file to be read, i.e. in the newInput method, the file isn't recognised, although it should be. Please try the program out for yourself to see what I mean. Is there something I'm doing wrong? Thank you very much in advance.Attached File  files.txt (13bytes)
Number of downloads: 54

Is This A Good Question/Topic? 0
  • +

Replies To: Input files not recognised

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Input files not recognised

Posted 18 March 2010 - 08:53 AM

Welcome to DreamInCode jayd!
Never use .eof() flag to check if you are at the end of the file. Read here: http://www.dreaminco...howtopic=145699
Secondly, you should use "mystdstring.c_str()" instead of "mystdstring.data()". It's safer, because c_str() returns a NULL terminated string (appends the NULL terminator), but .data() doesn't.
Furthermore, when you use "fstream" to deal with files, the second parameter must be the file opening mode. In your case, you need fstream::in (specifies to open files to read from them).
With the according changes, here's the fixed code:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

void newInput(const char* entry)
{
                cout << entry << endl;
		fstream currentFile (entry, fstream::in);
                if(!currentFile) 
                {
                        cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
                        exit(1);
                }
                else
                {
                        cout << "File Input" << endl;
                }
                currentFile.close();
}

int main(int argc, char *argv[])
{

        if (argc != 2)
    {
       cout << "Incorrect execution - please enter a text file (only one) next time you run the program. Program shall now exit.\n" << endl;
       return 0;
    }
        
        // fstream::in specifies opening mode
	fstream myFile (argv[1], fstream::in);
        string line;
        bool eofFound = false;

        if(!myFile) 
        {
            cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
            return 0;
        }

        // directly extracts lines from while condition
        while (getline(myFile, line))
		{
                //If it's not the EOF line, the file is input
                if (line != "EOF")
                {
		    newInput(line.c_str()); // >>>> C_STR instead of DATA
                }
                //If the line is the EOF line, we know that it is the end of the file
                else if (line == "EOF")
                {
                        eofFound = true;
                }
        }
        
        //If no 'EOF' is found, then an error is displayed and the program exits 
        if (!eofFound)
        {
                cout << "No EOF line in file - please check your input, and try again. Program shall now exit.\n";
                return 0;
        }
        //closes the file
        myFile.close();


        cout << "\nProgram finished." << endl;

        return EXIT_SUCCESS;
}


This post has been edited by sarmanu: 18 March 2010 - 08:57 AM

Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input files not recognised

Posted 18 March 2010 - 09:15 AM

Actually, the fstream constructor defaults to mode ios_base::in | ios_base::out if the mode isn't specified.

But anyway, even with your changes, the Test.cpp is still not being opened, although "Test.cpp" is being printed in the newInput function.

This post has been edited by r.stiltskin: 18 March 2010 - 09:16 AM

Was This Post Helpful? 1
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Input files not recognised

Posted 18 March 2010 - 09:19 AM

NVM just ignore the post, I don't really think I got what stilskin said ..

This post has been edited by sarmanu: 18 March 2010 - 09:22 AM

Was This Post Helpful? 1
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input files not recognised

Posted 18 March 2010 - 09:31 AM

View Postsarmanu, on 18 March 2010 - 11:19 AM, said:

NVM just ignore the post, I don't really think I got what stilskin said ..

Even after making your changes, it still doesn't work, although I haven't been able to figure out why. For some reason, even though a valid c-string is being passed to the newInput function, and even though I put a valid "Test.cpp" file in the current directory, the currentFile object has its failbit set.

Did the revised code work for you?
Was This Post Helpful? 1
  • +
  • -

#6 jayd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 10

Re: Input files not recognised

Posted 18 March 2010 - 09:39 AM

Hmm, no, the revised code didn't work for me, although I can't see why it shouldn't... Could converting the 'entry' into a string perhaps help? If so, how could I do it? Thanks for all the replies so far by the way, I greatly appreciate it.
Was This Post Helpful? 1
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Input files not recognised

Posted 18 March 2010 - 09:47 AM

The above code works perfectly fine for me. I don't know why it doesn't for you ... you could try this:
void newInput(string entry)
{
    cout << entry << endl;
    fstream currentFile (entry.c_str(), fstream::in);
 
    // more code
}


and call the function:
newInput(line);


Don't know if that changes something though ...
Was This Post Helpful? 1
  • +
  • -

#8 jayd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 10

Re: Input files not recognised

Posted 18 March 2010 - 10:01 AM

Hmm, no, it still doesn't want to work... Although you're right, it should be working.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input files not recognised

Posted 18 March 2010 - 10:06 AM

Got it. The problem is that the file "files.txt" that jayd uploaded was created on a windows machine so the lines end with CR LF. I'm working in Linux (and I'm guessing that jayd is too) but sarmanu is either working in Windows or created his own files.txt.

Anyway, for me at least, the string that's being read in and passed to the newInput function includes the CR (character 13) and therefore doesn't match the filename which of course doesn't contain a CR.

Modify your original code to use line.c_str() instead of line.data() as sarmanu suggested, and try my files2.txt (or rewrite files.txt with a text editor), and I think it will work for you.

Attached File(s)


Was This Post Helpful? 1
  • +
  • -

#10 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 175
  • Joined: 30-September 08

Re: Input files not recognised

Posted 18 March 2010 - 10:59 AM

I have had endless problems with windows and text files. There are so many problems introduced with windows and all of its formatting.
Was This Post Helpful? 1
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Input files not recognised

Posted 18 March 2010 - 12:34 PM

An easy way to deal with the possibility that there may be a stray CR before the LF is to add this line
line = line.substr( 0, line.find_first_of(13) );



immediately after the getline( myFile, line )
Was This Post Helpful? 0
  • +
  • -

#12 jayd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 10

Re: Input files not recognised

Posted 18 March 2010 - 02:46 PM

Yep, that
line = line.substr( 0, line.find_first_of(13) ); 
has done it, and it seems to be working now. Thanks for all the help everyone! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1