Read Text File Into Char Array C++
21 Replies - 54442 Views - Terminal Post: 29 April 2010 - 10:12 AM
#1
Read file into a char array
Posted 29 April 2010 - 05:17 AM
Is there a function in the C++ standard library that would let us to read a line in a text file and put that line into a char array?
I know of the getline method that would only allow us to insert data into a string.
Any ideas?
Replies To: Read file into a char array
#2
Re: Read file into a char array
Posted 29 Apr 2010 - 05:twenty AM
How big is char array?
#3
Re: Read file into a char array
Posted 29 Apr 2010 - 05:22 AM
well in the text file the line length is variable.
#iv
Re: Read file into a char assortment
Posted 29 April 2010 - 05:27 AM
That's why I asked.
There's Go line from stream merely it assumes the array is bigger than the text existence copied from the file.
In that location's Read cake of data
It's example code reads the whole file into an assortment at in one case.
This mail service has been edited past n8wxs: 29 April 2010 - 05:30 AM
#five
Re: Read file into a char assortment
Posted 29 Apr 2010 - 05:32 AM
one small question #four n8wxs. Is this for file input stream? I read the link posted past you. I think information technology's for keyboard input stream?
http://www.cplusplus...tutorial/files/ has a getline method that only accepts cord not char.
#half dozen
Re: Read file into a char array
Posted 29 April 2010 - 05:35 AM
No, cin is just an instance of the istream form. It'southward getline() method works for all istream objects.
#7
Re: Read file into a char array
Posted 29 April 2010 - 05:45 AM
So in my case since the line length is non constant i will take to utilize malloc, which increases the complexity. Am i right?
Isn't at that place much shorter method of doing this?
#8
Re: Read file into a char assortment
Posted 29 April 2010 - 05:51 AM
You could read the file twice with the string version of getline(). Count the lines read and and so create a string array that size. Reread the file into that array. No malloc() involved considering the string hides it.
This post has been edited by n8wxs: 29 April 2010 - 05:51 AM
#9
Re: Read file into a char array
Posted 29 April 2010 - 05:56 AM
okay i tried using getline to load the line into an assortment
char *arr; ifstream myfile ("test.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,arr); // Line A outer++; } myfile.close(); } else cout << "Unable to open file";
But i was unsuccessful.
Error message that i get
no matching function for phone call to `getline(std::ifstream&, char*&)'
at line A
How tin can i resolve this?
This mail service has been edited by Au-Z-C: 29 Apr 2010 - 05:57 AM
#10
Re: Read file into a char array
Posted 29 Apr 2010 - 05:58 AM
Take a wait at std::getline documentation. The second parameter must be a STL cord, non a arrow to char.
EDIT: never use .eof() function anymore. Read here why. So, simply apply getline within the while loop condition:
string s; // The getline inside the while loop both extracts // a line & checks if at that place is any data remaining. while (getline(my_file, s)) { // do any you want with s }
This post has been edited by sarmanu: 29 April 2010 - 06:00 AM
#11
Re: Read file into a char array
Posted 29 April 2010 - 06:04 AM
So isn't there is a directly mode to read a line into a char array?
#12
Re: Read file into a char array
Posted 29 April 2010 - 06:07 AM
Why would you want that? In that location is a direct manner to read into a char array, but it uses C functions. Take a look at fgets.
My advice: utilise std::getline with a STL string, then convert that string to a char arrow. You already know how to do it.
This post has been edited past sarmanu: 29 April 2010 - 06:07 AM
#xiii
Re: Read file into a char array
Posted 29 Apr 2010 - 06:08 AM
Wait at the reference I gave yous in mail service #four.
#14
Re: Read file into a char array
Posted 29 April 2010 - 06:11 AM
Indeed there is the istream::getline function, but it is simply not efficient, since the line size may vary.
#15
Re: Read file into a char array
Posted 29 Apr 2010 - 06:11 AM
Au-Z-C, on 29 April 2010 - 04:56 AM, said:
okay i tried using getline to load the line into an array
char *arr; ifstream myfile ("test.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,arr); // Line A outer++; } myfile.shut(); } else cout << "Unable to open file";
But i was unsuccessful.
Error bulletin that i get
no matching function for call to `getline(std::ifstream&, char*&)'
at line A
How can i resolve this?
You didn't classify an array, only a pointer that'southward uninitialized.
- ← Previous Topic
- C and C++
- Next Topic →
hollingsworthporser.blogspot.com
Source: https://www.dreamincode.net/forums/topic/171009-read-file-into-a-char-array/
0 Response to "Read Text File Into Char Array C++"
Post a Comment