HELP WITH - Display file from Command line

closed account (ivDwAqkS)
I am using a book to know about command line, however when I write the code's book example and call the text file as mentioned in the book(I already created the text file on the desktop) it doesn't execute the file and says that the file could not be opened

With this version of the program, you can use the command line to enter commands like this:
readfile2 output.txt

but when I just put

output.txt

it works, so my question is why the way of the book doesn't work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[]){
    int c;
    int i;
    char filename[MAX_PATH + 1];
    char input_line[MAX_PATH + 1];

    if(argc > 1)
        strncpy(filename, argv[1], MAX_PATH);
    else{
        cout << "Enter a filename and press ENTER: ";
        cin.getline(filename, MAX_PATH);
    }
    ifstream file_in(filename);

    if(!file_in){
        cout << "ERROR: " << filename << " could not be opened.";
        return -1;
    }
    while(true){
        for(i = 1; i <= 24 && !file_in.eof(); i++){
            file_in.getline(input_line, MAX_PATH);
            cout << input_line << endl;
        }
        if(file_in.eof())
            break;
        cout << endl << "More? (Press 'Q' to quit\nOtherwise press ENTER to continue: ";
        cin.getline(input_line, MAX_PATH);
        c = input_line[0];
        if(c == 'q' || c == 'Q')
            break;
    }
    return 0;
}
Last edited on
Do you know what program is doing, in each line?

Your book suggests, that you have created some kind of parser(the program/function, that allows you to process text input). However, you don't have this kind of thing in your code - look at line 15-17. You only ask for a filename. You don't expect a command ("readfile"), you only expect filename.

Either your book has an error, or you've made an error while writing this code. Or maybe you're writing code for example before, and new one(the correct code) awaits on next page ;)

Cheers!
closed account (ivDwAqkS)
MatthewRock, that's what I was thinking, because all before that is in the book is about binary files, and I don't really understand the command line argument D: because is totally the same as file mode >.> what I mean is that it does the same thing, but with different codes, and I think command argument is for another use, not for this. I don't know why he did put this example. ill show you some screenshots of the book so you could give me a better answer :D
http://i60.tinypic.com/f50gm0.png
2nd image
http://i59.tinypic.com/qxtjig.png
Actually book isn't wrong, but it's not clear either.

You can pass arguments to program(if you're linux user, you're used to it; if not, then I hope that book explained how it works)
This way, you don't need to open program in console and then provide input; you give input with program call. For example, in linux, you have a function:

cp [SOURCE] [DIRECTORY]

You can use this command in your terminal: you simply type in cp and provide arguments, and function(program) will be called with proper arguments. You don't waste time nor space for asking questions("Please, tell me the name of file you would like to copy").

This way, you can simply copy files, like this:

cp main.cpp bin/

//it means: copy main.cpp to folder bin

The book wants you to do the same with the program: it says that you can call its name(and the suggested name is readfile2), and then provide input.

Also, the "error" that you found isn't really an error. The string is true.

I tried to make it at least a bit more clear. If I failed, you may want to look at this thread:
http://www.cplusplus.com/forum/beginner/5404/

Duoas, experienced user, showed how to use program the way your book wants you to use it.

CHeers!
closed account (ivDwAqkS)
I am windows user, and about the error is not an error, but it confuses the reader, because when you test it and press enter it continues, and not exit.
and it still say that the file cannot be opened. :/
if someone could help with more info about this, I would appreciate.
Topic archived. No new replies allowed.