Guest_imported
New member
- Jan 1, 1970
- 0
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;
}
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;
}