Home > other >  C file handling IO errors
C file handling IO errors

Time:05-18

My code is as follows:

It's suppose to write 100 random numbers and save them into a file, then save them in my three arrays called: masiv1, masiv2 and masiv3.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
    ofstream outFile;
    ifstream inFile;
    string homeDir = getenv("HOME");
    homeDir = homeDir   "/testovFile.txt";
    outFile.open(homeDir);
    int masiv1[100],masiv2[100],masiv3[100];

    if(!outFile.fail()){
    for (int i = 0; i < 100;   i)
    {
        outFile << (rand() % 100) << "\n";
    }
    outFile.close();
    } else {cout << "File open failed!" << endl;}

    inFile.open(homeDir);
    if(!inFile.fail()){
    for (int i = 0; i < 100;   i)
    {
        inFile  >> masiv1[i];
        inFile  >> masiv2[i];
        inFile  >> masiv3[i];
    }

    for (int i = 0; i < 100;   i) {
     cout << masiv1[i] << endl;
    }
    inFile.close();
    } else {cout << "File open failed!" << endl;}

}

Output:

7
58
44
9
92
3
40
69
60
78
97
67
79
21
93
45
94
53
68
96
22
24
77
33
35
14
25
94
17
4
88
82
16
44
0
0
0
0
0
0
44026832
1
0
0
1833038584
1
33554432
64
47572408
1
47572428
1
1833038688
1
16
0
1
0
44026832
1
0
0
0
0
0
0
0
0
44026832
1
1833040336
1
16777234
28
48038656
1
1833039808
1
47576748
1501495297
1833041766
1
1920169263
1651076143
-1799393720
1
-1799393872
1
1833039039
1
1833039120
1
1833038960
1
47765896
1013383169
720977920
0
603620773
0

CodePudding user response:

The issue is caused, because when you use the >> operator, c move its cursor in the file with 1 position each time.

CodePudding user response:

There are several problems here:

  • You open the complete std namespace with using namespace std;
  • You use C-Style arrays and not std::array or std::vector
  • You are using file system specific mechanism for creating a path. Your code will not run on WIndows systems. Use std::filesystem and std::path
  • Always initialize all variables during definition.
  • And, the main error is that you do not check the return value of IO operations

So, always check, if an IO operation (for example with >>) works.

In your loop

    for (int i = 0; i < 100;   i)
    {
        inFile  >> masiv1[i];
        inFile  >> masiv2[i];
        inFile  >> masiv3[i];
    }

you are calling the extraction operator 300 times. For 100 elements in the file. And you do not check the result of IO operations.

And this you see also in your output.

Example:

Value 1 is assigned to masiv1[0]
Value 2 is assigned to masiv2[0]
Value 3 is assigned to masiv3[0]
Value 4 is assigned to masiv1[1]
Value 5 is assigned to masiv2[1]
Value 6 is assigned to masiv3[1]
Value 7 is assigned to masiv1[3]
Value 8 is assigned to masiv2[3]
Value 9 is assigned to masiv3[3]
 . . .
 . . .
Value 99 is assigned to massive3[32]
Value 100 is assigned to massive1[33]

Value 101 is not existing. IO operation fails. All further `>>` will be skipped

And as you can see. The last value that is written is in massive1[33]. So, the 34th value. Because you did not initialize the values, the rest is indeterminate.

And this you see also in your output.

So, please always check any result of an IO operation and then act accordingly.

Check can be done with for example if (infile). This works, because the bool operator of the std::stream has been overwritten. Please see here.

And the same will work with if (inFile >> masiv1[i]), because the inserter will return a reference to the std::ifstream for which it was called. And then the bool operator will kick in again.

Hope this helps

  • Related