-1

i am having difficulty reading a line of text from file to a char array.` assume that i have a text file named "sample.txt", and it contains only few words per line. here is what my code is:

char buffer[100];
ifstream file;
file.open("sample.txt")
file >> buffer;

this stops reading after space. I also tried:

file.getline(buffer,100);

but this does not give me the correct text. After the text, some random symbols were assigned to the remaining of the array.

any help would be deeply appreciated!

Edit: This char array is a temporary array. Im passing text to this array, and then pass it to a class data member

user215939
  • 101
  • 1
  • 4

3 Answers3

1

For every and any input operation, you must check the return value; otherwise you cannot know whether the operation succeeded and what it did.

If you are reading into a fixed buffer, you need to check the stream object and the count of extracted characters:

file.getline(buffer, sizeof buffer);
auto n = file.gcount();

if (file) {
  std::cout << "Read line with " << n << " characters: '";
  std::copy_n(buffer, n, std::ostream_iterator<char>(std::cout));
  std::cout << "'\n";
} else if (n > 0) {
  std::cout << "Read incomplete line with prefix '";
  std::copy_n(buffer, n, std::ostream_iterator<char>(std::cout));
  std::cout << "'.\n";
  file.clear();
} else {
  std::cout << "Did not read any lines.\n";
}

Note that the extracted count (file.gcount()) includes the null terminator, which basic_istream::getline writes into the output buffer. (So the maximal length of a line that can be read completely is sizeof(buffer) - 1.)


Alternatively, you can read into a dynamic string. This means that memory will be automatically allocated to hold each complete line, but it's a lot easier to reason about:

for (std::string line; std::getline(file, line); ) {
  std::cout << "Read one line: '" << line << "'\n";
}

Here we only check the success of the input operation, and we do this inside the loop condition. The number of extracted characters (this time excluding the null terminator) is precisely line.size() after the successful read.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • [Full demo](https://ideone.com/0Cydaj) – Kerrek SB Apr 23 '17 at 16:42
  • thank you for the elaborated answer, this is a little bit advanced for me. Some functions and loops statements I have never seen before, I will look up what each function does and try to understand each statement. – user215939 Apr 23 '17 at 17:05
0

It would be much easier to just use a std::string object to get the line content and later convert it to a char array:

std::string str;
std::getline(file,str);
const char* c = str.c_str();
jpo38
  • 19,174
  • 7
  • 62
  • 118
0

Why is this happening

Try printing out the array before doing getline. You'll notice that the zany symbols populate the whole array.

getline() will plop characters into your array from the input stream until either 100 characters are read, or the delimiting character is reached. If the delimiting character is reached before 100 characters, the rest of your length-100 array will point to uninitialized memory.

If you want to interact with streams in this way, (using a char array instead of a string) you'll have to decide how you want the remainder of the array to be initialized if you'd like to avoid nonsense.

Jacob Panikulam
  • 1,028
  • 1
  • 8
  • 11
  • Thank you! Is that possible to populate the array with all '\0'. And then read the text? But this char array is a temporary array, do I have to reinitialize every time I read ? – user215939 Apr 23 '17 at 16:40