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();
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.
fstring inFile;
inFile.open("Sometextfile.txt")
while(!inFile.eof()) // test for the end of file...
{
infile >> mydata;
std::cout << mydata << std::endl;
}
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.
#include <iostream>
#include <fstream>
usingnamespace 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;
}