X-code Problem

Joined
Jun 6, 2009
Messages
5
Reaction score
0
Points
1
Problem: How can I read from a file (Using C++)? I used this code but it does not work:Shouting: . I put it in the same path.

#include <fstream>
int main ()
{
int x;
ifstream fin ("data.in");
fin >> x;
ofstream fout ("data.out");
fout << x*2;
fin.close();
fout.close();
return 0;
}
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,770
Reaction score
2,110
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Assuming you are beginning to learn C++ programming, my one advice is to do defensive programming. That means to always check returns from functions and do error checking. So in this case, you should confirm that "fin" managed to open the file you wanted, and only then continue.

Please take a look at: Input/Output with files
 
OP
M
Joined
Jun 6, 2009
Messages
5
Reaction score
0
Points
1
Assuming you are beginning to learn C++ programming, my one advice is to do defensive programming. That means to always check returns from functions and do error checking. So in this case, you should confirm that "fin" managed to open the file you wanted, and only then continue.

Please take a look at: Input/Output with files

Thank you for your advice. But I use this also, but it did not work? so what can i do?
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top