Reading from a file using fstream.

This is a discussion on Reading from a file using fstream. within the C++ Programming forums, part of the General Programming Boards category; Hi guys. Can you please tell me why this code prints garbage and loops forever. Code: #include <iostream> #include <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
    21,652
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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

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