Text Files

Hello,
I am writing a code for fun, and want to further my knowledge in this area, but am getting stuck. I want to ave files to text files, and then load parts of them later, and determine which parts to skip over if they are already filled in, here is the code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string user, password, input;
char test;

int main()
{
ofstream out;
out.open("info.txt");
cout << "Input username: ";
getline(cin, user);
cout << "Input password: ";
getline(cin, password);
out << "Username: " << user << endl;
out << "Password: " << password << endl;
out.close();
out.close();

ifstream in;
in.open("info.txt");
getline(in, user);
getline(in, password);
cout << user << endl << password << endl;
cout << "Input password: ";
cin >> input;
if(input == password)
cout << "Welcome Back " << user;
else
cout << "Invalid password";
cin >> test;
in.close();
}

When I call the user or password, it puts the whole string though, not just the password you entered. Also, how do i do a check to skip over entering a username and password if there is one already there. This is probably a pretty simple fix, I just can't figure it out. Thanks to anyone who can help.
Update: Found a way to tell if file is empty or not. Also, I hope this helps anyone reading, this is a portable way to do this, and not the WIN32 only way, still looking how to edit certain parts of the 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
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string user, password, input;
char read;

int main()
{
	FILE *pFile;
	fstream in, out;
	pFile = fopen("info.txt", "r+");
	in.open("info.txt");
	fseek(pFile, 0, SEEK_END);
	int length = ftell(pFile);
	if(length == 0)
	{
		cout << "Empty";
	}
	while(!in.eof())
	{
		in.get(read);
		cout << read;
	}
	cout << endl;
	cin.get();
}
something like this also does the same thing.

1
2
3
4
5
6
7
8
fstring inFile;
inFile.open("Sometextfile.txt")

while(!inFile.eof())  // test for the end of file...
{
      infile >> mydata;
      std::cout << mydata << std::endl;
}
That's in there already. I'm having trouble loading specific parts of the file now, anyone have any suggestions?
Anyone know how to load and edit specific parts of the data if the first part gets ignored because the notepad file is not empty?
why does your code open the same file twice? What are we trying to do to the file? You are mixing c-style file calls with c++ style calls Just to find the length. When I write code I usually don't worry about the lengths of files. The following code will show you if a file is empty or not with out doing the code jumps you do in yours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
        fstream in_stream;
        char cChar=0;
        int nCount = 0;
        // may need flags on the open to get more info from the file.
        in_stream.open("info.txt");

        while(!in_steam.eof())
        {
                nCount++;
                cin >> cChar;
                cout << cChar;
         }
         
         cout << endl << "File had a length of " << nCount << endl; 
}
Topic archived. No new replies allowed.