Hello, i am trying to read the contents of a txt file that will store a variable. this txt file will only contain one or string number. so far i have gotten the program to store numeric values but i am unable to store a string.
the below code reads from a txt file which only has the number "100" in the first line. it stores 100 into the variable x.
i have edited the above code to save the name"John" in variable x. but it does not work. i really need the program to read and save hex values such as 3E8. I am using string incorrectly. any help offered would be appreciated.
thanks for you reply, but i tried running the program but it still does not work. i am using borlan c++. I am not allowed to use another compiler.. can you think of anything else that may be causing the problem.
Does it compile? If not what errors do you get? I'm not at all framilar will borlan. I can't see anything by looking at it. Sorry sir. Maybe if you get errors we can try and figure it out.
Edit: what is this string n,x;
I write it like this string n = ""; Or I write string n; I don't know maybe that is not what is the problem like I said I'm unfamiliar with what you are using.
I'm puzzled as to why you use a while() loop to read a single value.
But that aside, are you sure that the file is actually opened correctly?
After the open() statement, you could check good() or is_open() to verify that the file was successfully opened. Use a cout message to show the status.
Hi dude I tried to look again for you ...I noticed that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream.h>
#include <conio.h>
#include <string>
#include <fstream.h>
void main()
{
ifstream input;
input.open("temp/hex.txt",ios::in);
string n,x;
while(!input.eof())
{input>>n;
}
x=n; // you assign n to x
cout<<n; // you cout n...did you mean x? Or am I mistaken?
getch();
}
Also you don't really need to assign it it anything if you input n just cout << n you don't have to assign it to another variable.
Maybe it will help or maybe I'm wrong take it with a grain of salt I tried.