Thread: Basic C++ Question

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Basic C++ Question

    Hi guys, having a bit of trouble with this, whenever I specify a file to open it obviously isn't opening properly due to what is returned to the_file.is_open(). I can't see what's wrong with it, but I'm still learning so yeah... Cheers.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
     
    int main(int argc, char *argv[])
    {
    	std::string file_contents;
    	std::ifstream the_file;
     
    	if (argc == 2)
    	{
    		the_file.open(argv[1]);
    		std::cout<<"Attempting to open file stream for: "<<argv[1]<<std::endl;
    	}
    	else
    	{
    		std::cout<<"Too many or too few arguments.";
    		std::cin.get();
    		std::exit(0);
    	}
     
    	if (!the_file.is_open())
    	{
    		std::cout<<"The file couldn't be opened!";
    		std::cin.get();
    		std::exit(0);
    	}
    	else
    	{
    		std::cout<<"File stream open!"<<std::endl<<"Printing contents of file..."<<std::endl<<std::endl;
    		std::cout<<"Press enter to see file contents!";
    		std::cin.get();
    		system("cls");
    		getline(the_file,file_contents,'\0');
    		std::cout<<file_contents<<std::endl<<std::endl;
    		std::cout<<"File contents printed. Press enter to quit.";
    		the_file.close();
    		std::cin.get();
    	}
    	return 0;
    }
    This is what I get:

    Code:
    C:\Documents and Settings\patrick>"C:\Documents and Settings\patrick\Desktop\Coding\project\Debug\project.exe" C:\services.txt
    Attempting to open file stream for: C:\services.txt
    The file couldn't be opened!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that c:\services.txt exists and is readable?

    I copied and pasted your program into an editor and compiled it with g++, and it works just fine. I fed it with the source file itself, and it printed it both when in the same directory and when I specify the full pathname.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Yep, I'm sure , I even tried putting the file in the same directory as the executable and changed the path accordingly, still get the same error... What the hell is going on?!

    EDIT: GAH! You gotta be freakin kidding me... I'm an officially an idiot , turns out the services.txt was infact a services.log, I didn't have "Show file extensions on" and I was too lazy to create my own text document, so I saw that sitting in C:/ and assumed it was a .txt lol. All works fine now, thanks for compiling it and testing Matsp, probably would never have got that if you didn't :P
    Last edited by pobri19; 08-23-2008 at 06:59 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pobri19 View Post
    Yep, I'm sure , I even tried putting the file in the same directory as the executable and changed the path accordingly, still get the same error... What the hell is going on?!

    EDIT: GAH! You gotta be freakin kidding me... I'm an officially an idiot , turns out the services.txt was infact a services.log, I didn't have "Show file extensions on" and I was too lazy to create my own text document, so I saw that sitting in C:/ and assumed it was a .txt lol. All works fine now, thanks for compiling it and testing Matsp, probably would never have got that if you didn't :P
    First thing I ever do on a Windows machine that I "own/use" is to disable the stupid "hide extensions of known file types" - it causes so many problems that it's just insane.

    And yes, I've been there too trying to open a file that doesn't exist. Not so many days ago, I had my team lead to come and look at my "create process" code, because it wouldn't create the process. It turned out that there was a bit of text missing in the filename that I'd given in the build configuration for the second process. [I meant to write "somethingmemprocess.exe" but it turned by some stupid reason into "somethingmprocess.exe" - being a long name as well made it harder to proofread].

    It is usually the simple things that trip you up - files not called what you expect them to be called - rather than the code that is complicated and hard to write, where you concentrate and get it right.

    But often it's hard to find those simple mistakes, because you don't think you made such simple mistakes. This is were "Pair debugging" is really efficient - someone else there to spot the simple things that you overlook yourself because you already "KNOW" what it's supposed to say. I'm not too keen on pair-programming, but two people helping each other debug is definitely one of those situations where it works really well. [And a company dog that can just sit there and listen while the programmer explains what the code does until he/she gets to the point where he/she says "Ah, I'm an idiot, I know what the problem is"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM