M
M
Michael2016-01-08 10:28:54
C++ / C#
Michael, 2016-01-08 10:28:54

How to read data from a file into a line (there is 1 line in the file)?

There is a text file (it is known that it has 1 line) and you need to read it in string (any String ^, even string), but the number of spaces and characters is unknown. How to do it? getline() didn't help

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AtomKrieg, 2016-01-08
@AtomKrieg

Is this code not working?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
int main(int argc, char *argv[]) {
  ifstream file("text.txt");
  string str;
  getline(file, str);

  cout << str << endl;

  system("pause");
  return 0;
}

K
kstyle, 2016-01-08
@kstyle

fgets() but it's char*. you can also character by character

A
Alexander Ananiev, 2016-01-08
@SaNNy32

Do you write under .net? Then https://msdn.microsoft.com/en-us/library/db5x7c0d(...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question