argc, argc Command-line[HELP]

I am trying to open the file and read text from it. Book showed me the example but it is not working.


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

#define SCR_LINES  24

int main(int argc, char *argv[]){
    
    int num;
    int i;
    
    char filename[MAX_PATH + 1];
    char input_line[MAX_PATH + 1];
    
    if(argc > 1){
         strncpy(filename, argv[1], MAX_PATH);
         }
      else if(argc > 2){
           cout << "Error! " << endl;
           }
           else if(argc < 1){
                cout << "Error! " << endl;
                }   
         else{
             cout << "Input a file name and press ENTER: ";
             cin.getline(filename, MAX_PATH);
             }
             
          
          ifstream file_in(filename);
          
          if (! file_in){
                
                cout << filename << " couldn't be opened. " << endl;
                system("PAUSE");
                return -1;
                }
                
             while(true){
                         for(i = 1; i <= SCR_LINES && ! file_in.eof(); i++){
                               file_in.getline(input_line, MAX_PATH);
                               cout << input_line << endl;
                               }
                               
                      if( file_in.eof())
                      break;
                      cout << endl;
                      
                      cout << "More? (Press 'Q' and 'ENTER' to quit): ";
                      cin.getline(input_line, MAX_PATH);
                      num = input_line[0];
                      
                      if(num ==  'Q' || num == 'q'){
                             break;
                             }
                             }
                             
                             file_in.close();
                             
                             system("PAUSE");
                             return 0;
                             }               
                  



Book didn't added this 'else if' i did. It was one of the exercises. I understand everything here. But it just won't go.
Last edited on
I think that "the Book" wants to show you something more than just opening and reading something from a file.

This should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#inlcude<cstdio>
#include<iostream>
using namespace std;
FILE *file;
int main()
{             char str[10000];
               file=fopen("textfile.txt", "r+");
               while(!feof(file))   //this checks the end of the file -  (pointer)
               {       fgets(str, 10000, file);
                        cout<<str<<" ";
               }
               fclose(file);
               return 0;
}


I think that you want to write the name of the file (or the path) and to open the file with that name. Anyway, the program below opens a text file (named "textfile.txt") and read everything on it untill the eof-pointer. I didn't compile the code but it should work properly.
Last edited on
I don't want code like yours(new header cstdio) i haven't got it in book yet.
Topic archived. No new replies allowed.