-1

I have to use fstream to create a .txt file and write 0 to 10 in it, then use fstream again to read the file and sum all the integers together before outputting the sum to the console.

I can't find where I am making a mistake:

fstream myFile;
myFile.open("numbers.txt", ios::in | ios::out);

int sum = 0;
int number = 0;
string line;

if (myFile.is_open()) {
    for (int i = 0; i <= 10; i++) {
        myFile << i;
    }
    myFile.seekp(0, ios::beg);
    while (getline(myFile, line)) {
        myFile >> number;
        sum += stoi(line);
    }
}

cout << "Sum: " << sum << endl;
return 0;

I tried getline() and other methods, but it didn't work.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sahand
  • 3
  • 2
  • 2
    1. You write 1 to 10, not 0 to 10. 2. The stream pointer is at the file end, of course you can't read lines after the file end. – 273K Apr 11 '23 at 19:00
  • 1
    close and reopen the file – pm100 Apr 11 '23 at 19:00
  • 1
    In file streams the read and write tracking is the same. If you write to the end of the file, a subsequent read will be from the end of the file. And clearly there's nothing to read if you're at the end of the file. – user4581301 Apr 11 '23 at 19:03
  • I removed `ios::trunc` tried `myFile.close()` it's the same thing, tried to move the pointer by using `myFile.seekp(0)` still same. – Sahand Apr 11 '23 at 19:13
  • Update the code to show the version where you try to rewind the file. – user4581301 Apr 11 '23 at 19:22
  • 2
    You read a line into a variable `std::string line`, and then you go get another number from the file into a variable `int number`, and then you do convert the string to a number but disregard what you stored in `number`. As a result you're not adding together all the numbers. – Ben Voigt Apr 11 '23 at 19:24
  • Side note: Program crashes because of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/q/21567291/4581301) This will be irrelevant after fixing the bug Ben Voigt points out, but still something to watch out for in the future.. – user4581301 Apr 11 '23 at 19:31

2 Answers2

1

You are writing the numbers to the file without any delimiters between them, so the file will contain one long digit sequence in it, ie 012345678910. You won't be able to make sense of that, no matter how you read it. So, separate the numbers to make them easier to read.

More importantly, after writing to the file and seeking (the output position) to the beginning (rather than seeking the input position), you are then calling getline() to read the entire file into a std::string, and then you are trying to convert that entire string into an int, but the value 012345678910 exceeds the max value that int can hold (2147483647), so stoi() will fail.

You can try this instead:

fstream myFile;
myFile.open("numbers.txt", ios::in | ios::out);

int sum = 0;

if (myFile.is_open()) {

    for (int i = 0; i <= 10; i++) {
        myFile << i << ' ';
    }

    myFile.seekg(0, ios::beg);

    int number;
    while (myFile >> number) {
        sum += number;
    }
}

cout << "Sum: " << sum << endl;

Online Demo

However, note that when you are opening the file, you are specifying both the in and out flags. That will fail if the file does not already exist (even if it is blank). So, if you need to create a new file, the in flag must not be specified, but then you can't read from the file, only write to it. See this documentation for std::basic_filebuf::open() to understand why.

So, you would have to open the file for writing, then close and reopen it for reading, eg:

fstream myFile;

myFile.open("numbers.txt", ios::out);
if (myFile.is_open()) {
    for (int i = 0; i <= 10; i++) {
        myFile << i << ' ';
    }
    myFile.close();
}

int sum = 0;

myFile.open("numbers.txt", ios::in);
if (myFile.is_open()) {
    int number;
    while (myFile >> number) {
        sum += number;
    }
    myFile.close();
}

cout << "Sum: " << sum << endl;

Online Demo

In which case, you should use ifstream and ofstream separately, instead of using fstream, eg:

ofstream outFile("numbers.txt");
if (outFile.is_open()) {
    for (int i = 0; i <= 10; i++) {
        outFile << i << ' ';
    }
    outFile.close();
}

int sum = 0;

ifstream inFile("numbers.txt");
if (inFile.is_open()) {
    int number;
    while (inFile >> number) {
        sum += number;
    }
    inFile.close();
}

cout << "Sum: " << sum << endl;

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

There are two errors.

  1. When you switch between reading and writing of the same file, you must insert a seek operation in between.
  2. Using both getline(myFile, line) and myFile >> number; makes no sense. One of them must go.

Demo.

PS: the question has been changed, this answer may or may not be valid for its current state.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243