File output skips first line

I am learning C++ and have come across something that is confusing me. My goal is to read in a file, add some things to it, take some input then output all of this to a new file.

The problem is whenever I output all of the modified data, it will either output an extra line that I don't want, or it will output one less line than I want. For example, the input file will contain:

1
2
3
LineA
LineB
LineC


The output will be either:

1
2
3
4
:
LineA:
LineB:
LineC:


or:

1
2
LineB:
LineC:


I believe this problem is occurring in the for loop. I've tried changing the b value to 1 or 0 and both will not give me what I want.

Here is what I have so far.

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {

int a=0;
int b;

char input[256];

string line[32];

if(argc != 3) {

        cout<<argv[0]<<" <config> <output>"<<endl;
        }

else {

        ifstream config(argv[1]);

        while(!config.eof()) {
                a=a++;
                getline(config,line[a]);
                }

        ofstream file(argv[2]);

        for(b=0;b<a;b++) {

                file.open(argv[2],ios::app);
                line[b].append(": ");

                cout<<"["<<b<<"] "<<line[b];
                cin.getline(input,256);

                line[b].append(input);

                file<<line[b]<<endl;
                file.close();
                }
        }

return 0;
}


Thanks!
This method is error prone:

1
2
3
4
        while(!config.eof()) {
                a=a++;
                getline(config,line[a]);
                }


It is better to use this:

1
2
3
4
5
        std::string input;
        while(getline(config, input)) {
                a=a++;
                line[a] = input;
        }


The reason is that the EOF flag is not set until AFTER the read fails. So doing it your way will add at leas one input to the array before the end of file is detected.
Also, rather than using an array of strings it would be better to use a std::vector:

1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>

// ... stuff ...

std::vector<std::string> line;

std::string input;
while(std::getline(config, input))
{
    line.push_back(input); // only when std::getline() succeeded
}


http://www.cplusplus.com/reference/stl/vector/
Last edited on
1
2
3
4
5
6
7
 std::string input;
a=0;
while(getline(config, input)) {
          //a=a++; the value of a is undefined
          line[a] = input;
          a++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream.h>
#include<fstream.h>
#include<cstring.h>

void main()
{
	ifstream file;
	file.open("D:qabil.txt");
	string temp;
	getline(file,temp);//you can do this step until line you want
	
getline(file,temp,1);
	cout<<temp;
}
Thanks guys, still working on this. I get an error when compiling when I modify the while loop to contain:

while(getline(config,input))

This happens when I set:

string input

or

char input[256]

The error I'm getting:

1
2
In function `int main(int, char**)':
26: error: no matching function for call to `getline(std::ifstream&, char [256])'
Last edited on
Did you #include <string>? Also did you prefix your strings with std:: like I did?

1
2
3
4
5
6
7
8
9
#include <string>

// ... stuff ...

std::string input;

// ... stuff ...

std::getline(config,input);

Found the problem, was before the for loop.

I changed this:
ofstream file(argv[2]);

to this:
ofstream file;

and now it works. I don't completely understand why this was causing a problem, but after studying fstream a bit more, I noticed I was adding my argv[2] twice previously. One time before the for loop with ofstream and one time during the loop with open. Why would this cause an issue for what I was attempting?

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {

int a=0;
int b;

string input;
string line[32];

if(argc != 3) {

        cout<<argv[0]<<" <config> <output>"<<endl;
        }

else {

        ifstream config(argv[1]);

        while(getline(config,input)) {
                a=a++;
                line[a]=input;
                }

        ofstream file;
        file.open(argv[2],ios::trunc);

        for(b=1;b<=a;b++) {

                line[b].append(": ");

                cout<<"["<<b<<"] "<<line[b];
                getline(cin,input);

                line[b].append(input);

                file<<line[b]<<endl;
                }

        file.close();
        }

return 0;
}
Last edited on
Topic archived. No new replies allowed.