Thread: Reading from a file using fstream.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20

    Reading from a file using fstream.

    Hi guys.

    Can you please tell me why this code prints garbage and loops forever.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main (int agrc, char* argv[])
    {
    	fstream file;
    	fstream output;
    	
    	wchar_t *c1;
    
    	if (argv[1] == NULL)
    	{
    		cout << "Usage: Profiler <file-name.c>" << endl;
    		exit (1);
    	}
    	
    	file.open ("c:\258_assignment2\Profiling\ff.c", ios::in);
    	output.open ("output", ios::out);
    
    	
    	while (!file.eof() )
    	{
    		char c;
    		c = file.get();
    		cout << c << endl;
    		output << c << endl;
    	}
    
    	return 0;
    }
    I also have two questions:
    1- how can i check if the file opened successfully?
    2- i need to pass the argv[1] to open as filename, but i get an error about wchar_t type. I ca't seems to pass char*. How can i solve this?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can you please tell me why this code prints garbage and loops forever.
    Not sure why it loops forever, but
    1. You need to escape the backslashes in your input filename.
    2. You should not control the loop with !file.eof()
    You could consider file.good(), or just loop with while (file.get(c))

    1- how can i check if the file opened successfully?
    Checking for file.good() is one way.

    2- i need to pass the argv[1] to open as filename, but i get an error about wchar_t type. I ca't seems to pass char*. How can i solve this?
    It should take a char*, so I am not sure what is the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    Alright, thank you.
    It seems i was trying without the escaping, and i was also trying just ff.c, thinking it should work since they're in the same forlder.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM