×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

reading from files

reading from files

reading from files

(OP)
My humble thanks in advance.

Coming from easier languages like java, I am used to the idea of being able to read characters in from a file and then have the whole file in memory in a big string or byte array. But in c++ I've only managed cumbersome, round-about ways of doing this.

I tried reading characters with an ifstream using getline (see below), but in every move through the loop, fin always reads in characters in at the beginning of the character array. So I have the ridiculous setup below with an offset counter to copy the values from a little input array (c) to the appropriate position in a big storage array (content). There has to be an easier way. I know that this is pretty bad but please suggest something better.

#include <fstream.h>
int main()
    {
    char c[100];
    char content[10000];
    int counter = 0;
    ifstream fin("file.cpp");
    if (fin)
        {
        while(!fin.eof())
            {
            fin.getline(c, sizeof(c));
            for (int i=0; i<strlen(c); i++)
                content[i+counter] = c[ i ];
            counter += strlen(c);    
            }
        }
    cout << content;
    return 0;
    }

RE: reading from files

If you want to read each character in from the file you might try something like:

fin >> c;
// Process c into content, etc.


But if you are trying to read the whole thing into memory try:

fin.read(content, 10000);


The problem will be that big files may not fit into memory. One word pf warning, I haven't tested these but I'm pulling these from my memory (which tends to be faulty).

James P. Cottingham
www.ivcusa.com

RE: reading from files

(OP)
Great thanks JPC!
that worked. Now I have:

#include <fstream.h>
int main()
    {
    char content[10000];
    char fileName[] = "file.cpp";
    ifstream fin(fileName);
    if (fin) fin.read(content, sizeof(content));
    cout << content;
    return 0;
    }

Which works fine and the char array content holds the file content.

Now suppose I want to copy that char array to a standard string object. How would I do that?

Any help appreciated.

RE: reading from files

The easiest way is to do something like:

String strContent = content;


Going the other way is the kicker. You will need c_str. There are possible memory violations with this but most of the time it works.

content = strContent.c_str();


Don't forget to include the proper string header. Again, I'm doing this from memory and I may have forgotten something important.

James P. Cottingham
www.ivcusa.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close