Read file into a char array

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 16890 Views - Last Post: 29 April 2010 - 10:12 AM Rate Topic: -----

#1 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Read file into a char array

Posted 29 April 2010 - 05:17 AM

Is there a function in the C++ standard library that would allow 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?
Is This A Good Question/Topic? 0
  • +

Replies To: Read file into a char array

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 05:20 AM

How big is char array?
Was This Post Helpful? 0
  • +
  • -

#3 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Read file into a char array

Posted 29 April 2010 - 05:22 AM

well in the text file the line length is variable.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 05:27 AM

That's why I asked. :)

There's Get line from stream but it assumes the array is bigger than the text being copied from the file.

There's Read block of data

It's example code reads the whole file into an array at once.

This post has been edited by n8wxs: 29 April 2010 - 05:30 AM

Was This Post Helpful? 1
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Read file into a char array

Posted 29 April 2010 - 05:32 AM

one small question #4 n8wxs. Is this for file input stream? I read the link posted by you. I think it's for keyboard input stream?

http://www.cplusplus...tutorial/files/ has a getline method that only accepts string not char.
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 05:35 AM

No, cin is just an instance of the istream class. It's getline() method works for all istream objects.
Was This Post Helpful? 1
  • +
  • -

#7 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Read file into a char array

Posted 29 April 2010 - 05:45 AM

So in my case since the line length is not constant i will have to use malloc, which increases the complexity. Am i correct?

Isn't there much shorter method of doing this?
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 05:51 AM

You could read the file twice with the string version of getline(). Count the lines read and then create a string array that size. Reread the file into that array. No malloc() involved because the string hides it.

This post has been edited by n8wxs: 29 April 2010 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#9 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

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 array

 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 call to `getline(std::ifstream&, char*&)'

at line A

How can i resolve this?

This post has been edited by Au-Z-C: 29 April 2010 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#10 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Read file into a char array

Posted 29 April 2010 - 05:58 AM

Take a look at std::getline documentation. The second parameter must be a STL string, not a pointer to char.
EDIT: never use .eof() function anymore. Read here why. So, simply use getline inside the while loop condition:
string s;
// The getline inside the while loop both extracts
// a line & checks if there is any data remaining.
while (getline(my_file, s))
{
    // do whatever you want with s
}


This post has been edited by sarmanu: 29 April 2010 - 06:00 AM

Was This Post Helpful? 1
  • +
  • -

#11 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Read file into a char array

Posted 29 April 2010 - 06:04 AM

So isn't there is a direct way to read a line into a char array?
Was This Post Helpful? 0
  • +
  • -

#12 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Read file into a char array

Posted 29 April 2010 - 06:07 AM

Why would you want that? There is a direct way to read into a char array, but it uses C functions. Take a look at fgets.
My advice: use std::getline with a STL string, then convert that string to a char pointer. You already know how to do it.

This post has been edited by sarmanu: 29 April 2010 - 06:07 AM

Was This Post Helpful? 1
  • +
  • -

#13 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 06:08 AM

Look at the reference I gave you in post #4.
Was This Post Helpful? 1
  • +
  • -

#14 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

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.
Was This Post Helpful? 0
  • +
  • -

#15 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read file into a char array

Posted 29 April 2010 - 06:11 AM

View PostAu-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.close();
  }

  else cout << "Unable to open file"; 



But i was unsuccessful.

Error message that i get

no matching function for call to `getline(std::ifstream&, char*&)'

at line A

How can i resolve this?


You didn't allocate an array, just a pointer that's uninitialized.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2