read and store varible from text file

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream.h>
#include <conio.h>
#include <string>
#include <fstream.h>
 void main()
 {
  ifstream input;
  input.open("temp/hex.txt",ios::in);
  int n,x;
 while(!input.eof())

  {input>>n;

  }

  x=n;
  cout<<x;
  getch();
 }




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.

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;
  cout<<n;
  getch();
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>// changed .h
#include <conio.h>
#include <string>
#include <fstream> same thing here
using namespace std; //included 
void main()
 {
  ifstream fin; // I call it fin just preference
  fin.open("myfile.txt",ios::in);
  string n,x;
 while(!fin.eof())

  fin >> n;

  

  x = n;
  cout << n;
  getch();
 }


I got it to work on mine. I used call it fin instead of input I added using namespace std;
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.

Nevermind its the same.. Yeah I'm not sure dude.
Last edited on
Anyone else has any idea how I can get this working?
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.
Topic archived. No new replies allowed.