How to update the data in txt file

Im trying to update the data but I am not sure on how to update it.
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

ofstream myfile;
myfile.open("data.txt");

cout << "Update the existing data"<<endl;
    	std::cout << endl << "Please enter data : ";
	std::string data;
	getline(cin,data);


	while (data.empty() || data!= data)
		{

		if (std::getline(std::cin, data))
		{
		if (data.empty())
		{
		std::cout << "You pressed enter without typing anything" << std::endl;
		continue;
		}
						
		else if (data != data){
		std::cout << "There is no data. Please try again." << std::endl;
		}
		else if (data== data){
                 //trying to update the data when the user enter a correct data.
		}
					
		}
myfile.close();
Last edited on
Why do you think data != data will ever do anything useful?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ cat foo.cpp
#include<iostream>
#include<string>
using namespace std;
int main()
{
  string line;

  cout << "Enter something > ";
  getline(cin,line);
  cout << "empty=" << line.empty() << endl;
  cout << "self compare=" << (line != line) << endl;

  return 0;
}

$ ./a.out 
Enter something > hello
empty=0
self compare=0
$ ./a.out 
Enter something > 
empty=1
self compare=0

the reason is if the user enter the data that is not stored in the txt file , it will then prompt an error. Thats why I put data != data if the user enter a data that is not exist. The way you do does not apply myfile.open() btw. Just need to know how to update the data from my code.
Last edited on
> The way you do does not apply myfile.open() btw
I know.

But then again, neither were you.

But before we go anywhere near writing to the file, you need understand that comparing something with itself is trivially true - with the possible exception of floating point NaN's.

I tried my best on find the resources and do the code. However I'm still having an error which is [Error] no matching function for call to 'getline(std::ofstream&, std::string&)'. Need help here....

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
std::cout << endl << "Please enter data: ";
	std::string data;
	getline(cin,data);
	ifstream file;


		ofstream temp;
    		temp.open("temp.txt");
    		while (getline(myfile,data)) //this is the error i occured
    		{
      		if (data!= find)
        	temp << data<< endl;
    		}
    		myfile.close();
    		temp.close();
    		remove("data.txt");
    		rename("temp.txt", "data.txt");
				
			std::cout << endl << "Please enter name your name : ";
			std::string name;
			getline(cin,name);

                ofstream myfile;
    		myfile.open("data.txt", ios::app | ios::out);
		myfile<<name<<endl;
Last edited on
Hello rhap123,

I think what you need to do it describe, or post the instructions you were given, what the program needs to do. Also post any input file(s) that the program will need.

I am thinking the best course is to re-plan and rewrite the program instead of trying to fix parts of the code that do not work.

The code you have posted 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
ofstream myfile;
myfile.open("data.txt");  // <--- Opend file and erases anything in the file.

cout << "Update the existing data" << endl;  // <--- I think this is what you want, but it is also misleading based on the code that follows.
std::cout << endl << "Please enter data : ";  // <--- What kind of data?

std::string data;

getline(cin, data);

while (data.empty() || data != data)  // <--- The or part is not needed and will always return false.
{
    if (std::getline(std::cin, data))  // <--- Where is the prompt and why do you feel this is needed again this soon?
    {
        if (data.empty())  // <--- Already done in the while condition.
        {
            std::cout << "You pressed enter without typing anything" << std::endl;
            continue;
        }

        else if (data != data)  // <--- Useless. Always false.
        {
            std::cout << "There is no data. Please try again." << std::endl;
        }
        else if (data == data)  // <--- Always true.
        {
            //trying to update the data when the user enter a correct data.
            // <--- To soon to update data. You have done nothing to get something to update or changed anything.
        }

    }

    myfile.close();


And:
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
std::cout << endl << "Please enter data: ";

std::string data;

getline(cin, data);

ifstream file;  // <--- Defined file stream, but never open file.

ofstream temp;
temp.open("temp.txt");

while (getline(myfile, data)) //this is the error i occured  // <--- Where did you define and open "myfile"?
{
    if (data != find)  // <--- Where did you define "find"?
        temp << data << endl;
}

myfile.close();
temp.close();

remove("data.txt");
rename("temp.txt", "data.txt");

std::cout << endl << "Please enter name your name : ";

std::string name;

getline(cin, name);

The comments should explain most of what is wrong.

If you are going to bury the definition of a variable in the middle of other code. Use some blank lines to set it off so it can be found.

In line 12 of the second code not only is "myfile" not defined you are reading from the file into "data" which you have already given a value to. So how do you expect to find any match?

You have been given good information which you have mostly ignored except for the parts that you have copied and I am not sure if you understand what you have copied.

Andy
That is why Im having problem when updating the file. I have already managed to insert the data into the file. I understand what you meant but just need time to digest.
Topic archived. No new replies allowed.