MODIFY DATA FILE

Please help me out. I need to modify data files. The following code does not work, i.e., it re-writes the whole file. Can someone please tell me why? And also, please tell me how to modify data files. Thanks in advance :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<fstream>
#include<conio.h>
int main()
{
    int no1;
    std::fstream asdf;
    asdf.open("asd.txt");
    std::cout<<"enter the number: ";
    std::cin>>no1;
    for(int i=1; i<=5; i++)
    {
        if(i==no1)
        {
            asdf<<8;
        }
    }
    getch();
return 0;
}
Last edited on
When you open the file open with this argument: asdf.open("asd.txt", ios::app);. Then the file won't be re-written.

Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

int main()
{
    int no1;
    ofstream asdf;
    asdf.open("asd.txt", ios::app);
    cout << "Enter the number: ";
    cin >> no1;
    for(int i = 1; i<=5; i++)
    {
        if(i==no1)
        {
            asdf << 8;
        }
    }
    asdf.close();
    getch();
    return 0;
}
Thanks. That did help. But it doesn't modify the value.

Like, suppose I have these in the file asd:
1
2
3
4
5
1                      
2
3
4
5


and I input say, 5. I want it to read as
1
2
3
4
5
1                      
2
3
4
8

but what i get is
1
2
3
4
5
1                      
2
3
4
58


Please help me out. Thanks again
Last edited on
oh! And if i try entering 3, it still reads the same, i.e.,
1
2
3
4
5
1
2
3
4
58                                         
I assure you that this is much more easier in C than in C++ IMHO.
But I learn C++ and need to do a project in the same.. This is just the last part that I'm doing.
If we write 5 and them immediately after it 8, it would be read as a single number 58.

To write 5 and 8 as two different numbers, we must separate them with whitespace. Write 5, whitespace, 8; and then read it back as two numbers 5 and 8.

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

int main()
{
    const char* path = "asd.txt" ;

    // overwrite the file with new values
    {
        std::ofstream file(path) ;
        for( int i : { 12, 14, 61, 32, 56 } ) file << i << '\n' ;
    }

    // to check it out: dump the contents of the file to stdout
    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;


    // append new values to end of file
    {
        std::ofstream file( path, std::ios_base::app ) ;
        for( int i : { 77, 55, 88 } ) file << i << '\n' ;
    }

    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;

    // modify values in memory and re-write the file with the new values
    {
        std::vector<int> values ;

        // read the contents into memory
        {
            std::ifstream file(path) ;
            int i ;
            while( file >> i ) values.push_back(i) ;
        }

        // modify the contents in memory as desired

        std::reverse( values.begin(), values.end() ) ; // reverse the order

        values.push_back(99) ; // append 99 at the end

        // replace 61 with 666
        std::replace( values.begin(), values.end(), 61, 666 ) ;

        // append values if they are not already present
        for( int i : { 12, 22, 32, 42 } )
            if( std::find( values.begin(), values.end(), i ) == values.end() )
                values.push_back(i) ;

        // overwrite the file with the modified contents
        {
            std::ofstream file(path) ;
            for( int i : values ) file << i << '\n' ;
        }
    }

    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;
}

http://coliru.stacked-crooked.com/a/e382b6c0fb19349f
OK then I am writing a code on this now. Will let you know if it works.
@JLBorges Sorry! But I'm not allowed to use a vector, because I don't have it in my syllabus. Can i do it with an array?
Last edited on
OK, I've done the code and it works. You have to first get the data from the file and store it in an array then change the value in the array according to the number the user inputs then truncate the file and write the data to the file from the array.

Here it is:

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

using namespace std;

int main() {
    string data;
    string data_arr[5];
    int usrinput;
    
    ifstream asdf;
    asdf.open("asd.txt");
    
    for(int i = 0; i<=4; i++) {
        getline(asdf, data, '\n');
        data_arr[i] = data;
    }
    
    asdf.close();
    
    cout << "Enter a number: ";
    cin >> usrinput;
    
    for(int j = 1; j<=5; j++) {
        if(j == usrinput) {
            data_arr[j-1] = "8";
        }
    }
    
    ofstream asdfx;
    asdfx.open("asd.txt", ios::trunc);
    
    for(int k = 0; k<=4; k++) {
        asdfx << data_arr[k] << "\n";
    }
    
    asdfx.close();
    
    return 0;
}


Hope it helps.
Last edited on
> But I'm not allowed to use a vector, because I don't have it in my syllabus

What do you mean? Didn't you say earlier: "But I learn C++ and need to do a project in the same.."?


> Can i do it with an array?

You certainly can; but the code would become a lot messier. Harder to write correctly, harder to understand, test and debug.

If you have just started out learning C++, use std::vector<>. It is of no great concern that it hasn't been taught; to learn how to start using std::vector<> is a snap. Read this tutorial, and you would be ready to go in a very short time. http://www.mochima.com/tutorials/vectors.html
@Stormboy - :/ That didn't work for me. I don't know why. It didn't write the value "8" into the file.

@JLBorges - Vector is the only thing I cannot use. It's a project I'm doing for school, so I can't use it.
It worked for me. The file name should be asd.txt and should contain:
1
2
3
4
5
1
2
3
4
5


to work. If the file contains more lines you should change the values in the 'if statement' and the array size (of data_arr) respectively.
> It's a project I'm doing for school, so I can't use it.

Why? std::vector<> is a staple of programming in C++. Why can't you use it if you are actually learning to program in C++?


If there is a known upper limit on the maximum number of values that the file may contain, the array solution is straightforward:

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

int main()
{
    const char* path = "asd.txt" ;

    // overwrite the file with new values
    {
        std::ofstream file(path) ;
        for( int i = 10 ; i < 50 ; i += 5 ) file << i << '\n' ;
    }

    // to check it out: dump the contents of the file to stdout
    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;

    // append new values to end of file
    {
        std::ofstream file( path, std::ios_base::app ) ;
        for( int i = 70 ; i < 100 ; i += 10 ) file << i << '\n' ;
    }

    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;

    // modify values in memory and re-write the file with the new values
    {
        const std::size_t MAX_SIZE = 1024 ; // maximum possible number of values
        std::size_t actual_size = 0 ; // actual number of values, initially zero
        int values[MAX_SIZE] ;

        // read the contents into memory
        {
            std::ifstream file(path) ;
            int v ;
            while( file >> v && actual_size < MAX_SIZE )
                values[ actual_size++ ] = v ;
        }

        // modify the contents in memory as desired

        std::reverse( values, values+actual_size ) ; // reverse the order

        if( actual_size < MAX_SIZE ) // if there is space
            values[ actual_size++ ] = 500 ; // append 500 at the end

        // replace 25 with 666
        std::replace( values, values+actual_size , 25, 666 ) ;

        // add 30 to every value
        for( std::size_t i = 0 ; i < actual_size ; ++i ) values[i] += 30 ;

        // overwrite the file with the modified contents
        {
            std::ofstream file(path) ;
            for( std::size_t i = 0 ; i < actual_size ; ++i ) file << values[i] << '\n' ;
        }
    }

    std::cout << std::ifstream(path).rdbuf() << "-------------\n" ;
}

http://coliru.stacked-crooked.com/a/9a7fbcf1b9c84259
Topic archived. No new replies allowed.