I need to know the syntax of how i can read a string from a file without using char array or char variable.
i want to declare a variable of string type but its giving error .
I have include <cstring>,<ctype> libraries.

int main()
  string s1;
  ifstream fin
  while( !fin.eof())
    fin>>s1;   // this line is giving error


  return 0;

Recommended Answers

All 2 Replies

You have many errors. Firstly, click the code tags button instead of typing them in (you need a back slash on the close code) You are missing a semi-colon [;] on line 4. Next, you have not included fstream. Also, you need to change the line you say "is giving error". You cannot simply write backwards and forwards. Check out this link:
Also, you need to add

using namespace std;

under your includes, as you need to tell the compiler that you are using the standard reference for string.

#include <iostream>
using namespace std;
void main()
    string a;
    ofstream file("test.txt");              // for writing a string in file
    ifstream f("test.txt");
        getline(f, a);                   // for reading a string from file
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.