Sorting? Or Searching

Pages: 1234... 7
Yes but the file currently has nothing in it as all my efforts to output have failed so I can get any information if I haven't saved anything there?
Yes but the file currently has nothing in it as all my efforts to output have failed so I can get any information if I haven't saved anything there?


1
2
3
4
5
 ofstream File("test.txt");
	string f;
	cin >> f;
	File << f;
 

shouldn't that work?
Last edited on
closed account (D80DSL3A)
Yes it should. Your code (unchanged) results in a file name test.txt being created in my main project folder containing the string entered via the console on my computer.

I don't know why it isn't working for you. Are you looking for the file in the right place? Where it is created may depend on what IDE you're using, so check various folders in your project folder.
I cant run the code. It gives me the error on the file << f;


no operater matches these operands?
closed account (D80DSL3A)
There is no line like this:file << f; in the code above. Did you mis-name something?
1
2
3
4
ofstream File("test.txt");
	string f;
	cin >> f;
	File << f;


im talking about line 4?
Last edited on
ok it has randomly worked now . I have no idea why. C++ will always be confusing me xD . Also do you not sleep :D
closed account (D80DSL3A)
I can't tell from just that. That code works fine for me.
Can you post the actual error message? The details can be important.
A bit more of your code may help too, for judging context.
Last edited on
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
void DataEntry(Details &member){
	ofstream DataSave("test.txt");
	cout << "First Name: ";
    cin >> member.FirstName;
	DataSave << member.FirstName<<"\n";

    cout << "Last Name: ";
    cin >> member.LastName;
	DataSave << member.LastName<<"\n";

	cout << "Age(Please Enter an Integer(Number)): ";
    cin >> member.Age;
	DataSave << member.Age<<"\n";

	cout << "Email: ";
    cin >> member.Email;
	DataSave << member.Email<<"\n";

    cout << "Door Number(Please Enter an Integer(Number)): ";
    cin >> member.DoorNumber;
	DataSave << member.Age<<"\n";

    cout << "Road Name: ";
    cin >> member.RoadName;
	DataSave << member.RoadName<<"\n";

    cout << "Post Code(NOTE:Please use Lower Case): ";
    cin >> member.PostCode;
	DataSave << member.PostCode<<"\n";
}



This is overwriting what was in the text file before?
Last edited on
closed account (D80DSL3A)
Not really. The data should get written to the file OK, but it would be all run together.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
ofstream DataSave("test.txt");
	cout << "First Name: ";
    cin >> member.FirstName;
	DataSave << member.FirstName;

    cout << "Last Name: ";
    cin >> member.LastName;
	DataSave << member.LastName;

	cout << "Age(Please Enter an Integer(Number)): ";
    cin >> member.Age;
	DataSave << member.Age;

Might produce
DavyJones42
in the file.
Later, this would appear as a single string. You will want to insert newlines '\n' between elements.
eg: DataSave << member.Age << '\n';
Inserting a blank space instead would also separate the values enough so they could be read later.
Yes I actually did that before you told me ( I think im getting slightly better) the new lines that is


It gets written to the file. However after I close it and build and run the program again it gets overwritten.


I suppose this is because im not feeding/inputting that info that was saved back into the program? If I did it would stay right?
Last edited on
Also should I be outputting the data type to the file too eg if its the postcode should I put post code before it so when it gets red it saves to the appropriate place? I don't know.
closed account (D80DSL3A)
Yes, the file will be overwritten each time the program is run. If you rename the file between runs then a new "test.txt" will be created.
Also, you could easily modify that code to prompt the user for a filename to create.
1
2
3
string filename;
cout << "Enter a name for the output file to create: "; cin >> filename;
ofstream DataSave( filename.c_str() );
closed account (D80DSL3A)
I suppose this is because im not feeding/inputting that info that was saved back into the program? If I did it would stay right?

Yes. If there is an existing file (created in the last program run), you could open it and read the data for your Details objects directly from it.
If I prompted the user for a filename would I be able to prompt user to later load chosen file?
also
john
grace
20
johnman@hotmail.com
20
stcatherines
lu31ul


that's what the file currently looks like.

should I somehow be making it so the file stores it with
Name:John etc
or would I be able read this information fine later without having to do that.
closed account (D80DSL3A)
If I prompted the user for a filename would I be able to prompt user to later load chosen file?

I suppose you could keep a file with the names of all files previously created by this program and then present this list (of filenames) to the user, but this complication should come later.


should I somehow be making it so the file stores it with
Name:John etc
or would I be able read this information fine later without having to do that.

No. You should place only the data to be read in the file (otherwise you get into complicated data parsing).
The data format you showed first should work fine.
Last edited on
ok thanks

best get cracking with reading the data now.
closed account (D80DSL3A)
Great.
What it seems you are trying to do is to read data from a file if it already exists, or get the data from the user if no file exists yet, and create a file from that.
Something like this should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream File_in("test.txt");// attempt to open file for reading
    if( File_in.is_open() )
    {
        // the file exists and you can read data from it
    }
    else// opening "test.txt" for reading failed because "test.txt" does not exist
    {
        ofstream File_out("test.txt");// create the file and open it for writing
        if( File_out.is_open() )
        {
            // prompt user for data and write it to File_out.
        }
    }
Last edited on
Quick question when its reading how can I get it to read integers as integers instead of string as I am getting it to read it all in a loop?
Last edited on
Pages: 1234... 7