Seekg and Seekp program

Hi guys, I'm trying to write a program that reads from a file using seekg/seekp.
I'm just working on menu option #1 first to simply read from the file and display and I can't seem to get the data to display. I am, however, getting the line "Reading contents from the file below:", but no data from the actual file.
text file is in the same location as source code on the E: drive.

Any thoughts?

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

const int SIZE = 20;                     
typedef char String[SIZE];

//prototypes
void Menu(int &);
void ReadFile(ifstream &);
void ShowContents(ofstream &,String[],String[],int[],char[],const int);



int main()
 {  
    char filename[]= "E:\\shapes.txt";
    String fname[SIZE], lname[SIZE];
    int score[SIZE];
    char grade[SIZE];
    int choice;
    
    ifstream InList;
    ofstream OutList;
    InList.open(filename);
    OutList.open(filename);
    
    
    if(InList.good() && OutList.good())
    {
         do
         {         
         Menu(choice); 
         switch(choice)
        	{
            case 1:  
                   ReadFile(InList);
            /*case 2:
            
            case 3:
             
            case 4:
            
            case 5:*/
            }
         }while (choice != 5);                         
    }
	else
         cout << "File did not open successfully." << endl << endl;
          
          
    system("PAUSE");
    return EXIT_SUCCESS;

}
 
    void Menu(int &choice) 
    { 
         cout << "\n    Choose an option:";
	     cout << "\n...................................";
         cout << "\n 1- Display All Content of the File";
         cout << "\n 2- Display Content in Reverse Order";
         cout << "\n 3- Display from Point A to Point B";
         cout << "\n 4- Display from Point B to Point A";
         cout << "\n 5- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 


   void ReadFile(ifstream& inFile)
   {
        string data;
        if(inFile.fail())
	    {
		     inFile.open("shapes.txt");
	    }
	
        cout<< "\nReading Contents from the File below:" <<endl;
        while(inFile)
        {
             getline(inFile, data);
             cout << data << endl;   
        }
    }
When I try to read a txt file, the data doesn't appear on the console, but when I go to look at the file in the directory after I've tried to run the file it appears like it's now junk ?? see pre and post below. WTH? Any ideas?

1. why won't my file contents display on the console?
2. what in the world is happening to my data after I run the program?

Text file before running the program:
Terri Pinon 123123 100.00 20 4030.00 201.50 604.50 80.60 3143.40
John Smith 11111 50.00 15 2022.50 101.13 303.38 40.45 1577.55
Jane Doe 555555 50.00 10 2015.00 100.75 302.25 40.30 1571.70
Kate Jones ABC123 40.00 10 1615.00 80.75 242.25 32.30 1259.70
Lynn Jackson 121212 30.00 10 1215.00 60.75 182.25 24.30 947.70

After running the program (with data never appearing in the console):
ØS  137
ìS ‰ 2490368
ØS 2653656 &
%à¦~ °[& 2292872
 Ä 1994415412 à
à}( 1994415459 Ï
ëS 2124866097 (
² &áÜvñ dþÿÿÿc]àvpZàv@ 0
þÿÿÿ @ 2502284
 2490368
pZàv ý" 2501936
à}( 0
0
¼ý" T`ßv 2501936
þÿÿÿ  1994362564
àS 2502208
͘Ðv 2147352624
7iàvðS 2488
ù­ÐvC®ÐvÐþ" 2147344384
ÕŒÒv„ ¦ZþÿÿÿÚ˜Ðvú ÑvPayrollProgram2.txt 4]àvPS 2501936
Just one question. What is the purpose of OutList and why does it use the same filename as InList?
There was originally writing to the file, but then was just simplified to read for now. I'll remove it. I didn't think it was in the way and had planned to remove it before I was done. Do you think that is causing my issues?
Definitely that was the cause. When I ran the program, as soon as OutList was opened, all data from the original file was lost, which is the default behaviour unless you open it in some specific mode such as append etc.
oh wow it did work! I never thought the ofstream would do anything until I used it to write. thanks Chervil!
on to menu option 2....
So I'm trying option 2 now trying to read the file backwards from the last character, but I'm not getting data displayed to the console.

in the last function I set the read position to the end of the file, then get that byte, then go back one position and get that byte, etc. Could it have something to do with reopening the file? I think I handle that with the .fail() at the start of the function. Thoughts?


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

const int SIZE = 20;                     
typedef char String[SIZE];

//prototypes
void Menu(int &);
void ReadFile(ifstream &);
void ShowContents(ofstream &,String[],String[],int[],char[],const int);
void BackwardFile(ifstream &);


int main()
 {  
    char filename[]= "grades2.txt";
    String fname[SIZE], lname[SIZE];
    int score[SIZE];
    char grade[SIZE];
    int choice;
    
    ifstream InList;
    InList.open(filename);
    
    if(InList.good())
    {
         do
         {         
         Menu(choice); 
         switch(choice)
        	{
            case 1:  
                   ReadFile(InList);
                   break;
            case 2:
                   BackwardFile(InList);
                   break;
            /*case 3:
             
            case 4:
            
            case 5:*/
            }
         //InList.close();
         }while (choice != 5);                         
    }
	else
         cout << "File did not open successfully." << endl << endl;
          
          
    system("PAUSE");
    return EXIT_SUCCESS;

}
 
 

    void Menu(int &choice) 
    { 
         cout << "\n    Choose an option:";
	 cout << "\n...................................";
         cout << "\n 1- Display All Contents of the File";
         cout << "\n 2- Display Content in Reverse Order";
         cout << "\n 3- Display from Point A to Point B";
         cout << "\n 4- Display from Point B to Point A";
         cout << "\n 5- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 
    

   void ReadFile(ifstream& inFile)
   {
        char byte;
        if(inFile.fail())
	    {
		     inFile.open("grades2.txt");
	    }
	
        cout<< "\nReading contents from the file:" <<endl;
        if(inFile)
        {
             inFile.get(byte);
             while(inFile.good())
             {
                  cout << byte;
                  inFile.get(byte);  
             }
         }
         inFile.close();
    }
                        
                             
   void BackwardFile(ifstream& inFile)
   {
        char byte;
        if(inFile.fail())
	    {
		      inFile.open("shapes.txt");
	    }
	
        cout<< "\nReading contents backwards from the file:" <<endl;
        
        inFile.seekg(0L, ios::end);
        
        while(inFile)
        {
             inFile.get(byte);               
             cout << byte;
             inFile.seekg(-1L, ios::cur);  
        }
        inFile.close();
    }
These lines are suspicious:
1
2
3
4
        if(inFile.fail())
	    {
		      inFile.open("shapes.txt");
	    }


If the file has already been opened successfully, the only thing required is this:
 
    inFile.clear(); // reset all error flags 


Though to be honest, your approach seems like an awful lot of hard work.
I'd just read the entire file line by line into an array, and then display the array in the required order.
Last edited on
unfortunately, i can't use arrays for this assignment. :( just learning to use seek and tell, etc. I'll clear those suspicious lines and replace with clear to see if that helps.
Topic archived. No new replies allowed.