Hello C Board,
I'm a first time caller I reading thru C++ Without Fear-Overland. Okay, here's the example exercise I'm having a problem with:
Okay, that is the sample answer, i didn't change it.Code://the project name is readtxt2 and the text file is name output.txt #include <stdafx.h> #include <iostream> #include <fstream> #include <string.h> using namespace std; int main(int argc, char *argv[]) { int c; // input character int i; // loop counter char filename[81]; char input_line[81]; if (argc > 1) strncpy(filename, argv[1], 80); else { cout << "Enter a file name and press ENTER: "; cin.getline(filename, 80); } ifstream file_in(filename); if (! file_in) { cout << "File " << filename << " could not be opened."; return -1; } while (1) { for (i = 1; i <= 24 && ! file_in.eof(); i++) { file_in.getline(input_line, 80); cout << input_line; } if (file_in.eof()) break; cout << endl << "More? (Press 'Q' and ENTER to quit.)"; cin.getline(input_line, 80); c = input_line[0]; if (c == 'Q' || c == 'q') break; } return 0; }
I am told I can enter in the command line this:
readtxt2 output.txt
and it should open output.txt and display for me, but when I run it and enter:
readtxt2 output.txt
I am told file not cannot open, now when I just enter:
output
it works fine. why is that?
btw I'm using microsoft visual studio 2010 on windows vista.
I know this is a beginner question, so please bear with me.
I can use all the help I can get, since I am trying to learn by homeschool, thanks.