Read File and store in a array of chars - c++
up vote
0
down vote
favorite
I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:
int main()
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(getline(myfile,line1))
ReadFile(myfile);
And It works.
When I use an array of chars, I code like this:
int main()
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(myfile.peek()!EOF)
line1[size]->ReadFile();
The function ReadFile is this:
void ReadFile(ifstream &is)
char aux[100];
is.getline(aux,100);
c++ arrays char readfile ifstream
|
show 7 more comments
up vote
0
down vote
favorite
I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:
int main()
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(getline(myfile,line1))
ReadFile(myfile);
And It works.
When I use an array of chars, I code like this:
int main()
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(myfile.peek()!EOF)
line1[size]->ReadFile();
The function ReadFile is this:
void ReadFile(ifstream &is)
char aux[100];
is.getline(aux,100);
c++ arrays char readfile ifstream
myfile.peek()!EOF
doesn't seem right.line1[size]->myfile;
is also weird.
– Quimby
Nov 10 at 18:09
1
Don't use an array of chars then - a string is the correct thing to use here. Also,char line1[size];
is not valid C++ code.
– Neil Butterworth
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12
|
show 7 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:
int main()
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(getline(myfile,line1))
ReadFile(myfile);
And It works.
When I use an array of chars, I code like this:
int main()
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(myfile.peek()!EOF)
line1[size]->ReadFile();
The function ReadFile is this:
void ReadFile(ifstream &is)
char aux[100];
is.getline(aux,100);
c++ arrays char readfile ifstream
I'm trying to open a file and save the information there in an array of chars, however I'm not getting it. To save in a string use this:
int main()
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(getline(myfile,line1))
ReadFile(myfile);
And It works.
When I use an array of chars, I code like this:
int main()
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile)
cout<<"Unable to open the file."<<endl;
exit(0);
while(myfile.peek()!EOF)
line1[size]->ReadFile();
The function ReadFile is this:
void ReadFile(ifstream &is)
char aux[100];
is.getline(aux,100);
c++ arrays char readfile ifstream
c++ arrays char readfile ifstream
edited Nov 10 at 18:36
asked Nov 10 at 18:08
Alice
97
97
myfile.peek()!EOF
doesn't seem right.line1[size]->myfile;
is also weird.
– Quimby
Nov 10 at 18:09
1
Don't use an array of chars then - a string is the correct thing to use here. Also,char line1[size];
is not valid C++ code.
– Neil Butterworth
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12
|
show 7 more comments
myfile.peek()!EOF
doesn't seem right.line1[size]->myfile;
is also weird.
– Quimby
Nov 10 at 18:09
1
Don't use an array of chars then - a string is the correct thing to use here. Also,char line1[size];
is not valid C++ code.
– Neil Butterworth
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12
myfile.peek()!EOF
doesn't seem right. line1[size]->myfile;
is also weird.– Quimby
Nov 10 at 18:09
myfile.peek()!EOF
doesn't seem right. line1[size]->myfile;
is also weird.– Quimby
Nov 10 at 18:09
1
1
Don't use an array of chars then - a string is the correct thing to use here. Also,
char line1[size];
is not valid C++ code.– Neil Butterworth
Nov 10 at 18:10
Don't use an array of chars then - a string is the correct thing to use here. Also,
char line1[size];
is not valid C++ code.– Neil Butterworth
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
To read in an array of characters, or text, you can use std::getline
and std::string
:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
Process_Text(text);
Don't use arrays of characters, as they can overflow. Also, instead of using ==
to compare, you'll have to use strcmp
. Always verify that your array of characters is terminated by a nul character, ''
, otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = 0; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
const char letter = text[index];
++frequency[letter];
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
To read in an array of characters, or text, you can use std::getline
and std::string
:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
Process_Text(text);
Don't use arrays of characters, as they can overflow. Also, instead of using ==
to compare, you'll have to use strcmp
. Always verify that your array of characters is terminated by a nul character, ''
, otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = 0; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
const char letter = text[index];
++frequency[letter];
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
add a comment |
up vote
0
down vote
To read in an array of characters, or text, you can use std::getline
and std::string
:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
Process_Text(text);
Don't use arrays of characters, as they can overflow. Also, instead of using ==
to compare, you'll have to use strcmp
. Always verify that your array of characters is terminated by a nul character, ''
, otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = 0; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
const char letter = text[index];
++frequency[letter];
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
add a comment |
up vote
0
down vote
up vote
0
down vote
To read in an array of characters, or text, you can use std::getline
and std::string
:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
Process_Text(text);
Don't use arrays of characters, as they can overflow. Also, instead of using ==
to compare, you'll have to use strcmp
. Always verify that your array of characters is terminated by a nul character, ''
, otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = 0; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
const char letter = text[index];
++frequency[letter];
To read in an array of characters, or text, you can use std::getline
and std::string
:
std::string text;
std::getline(myfile, text);
To process text lines in a file:
std::string text;
while (std::getline(myfile, text))
Process_Text(text);
Don't use arrays of characters, as they can overflow. Also, instead of using ==
to compare, you'll have to use strcmp
. Always verify that your array of characters is terminated by a nul character, ''
, otherwise the string functions will go beyond your array, not stopping until a nul is found.
Edit 1: Space separated
To read in text that is space separated, use:
std::string text;
myfile >> text;
Edit 2: Counting characters in a string
You can count characters in a string by using another array.
unsigned int frequency[128] = 0; // Let's assume ASCII, one slot for each character.
// ... read in string
const size_t length(text.length());
for (size_t index = 0; index < length; ++index)
const char letter = text[index];
++frequency[letter];
edited Nov 10 at 19:04
answered Nov 10 at 18:20
Thomas Matthews
43.8k1168120
43.8k1168120
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
add a comment |
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
And then I can use the string text to count each char have the text?
– Alice
Nov 10 at 18:23
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
See my Edit 2.
– Thomas Matthews
Nov 10 at 19:04
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241941%2fread-file-and-store-in-a-array-of-chars-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
myfile.peek()!EOF
doesn't seem right.line1[size]->myfile;
is also weird.– Quimby
Nov 10 at 18:09
1
Don't use an array of chars then - a string is the correct thing to use here. Also,
char line1[size];
is not valid C++ code.– Neil Butterworth
Nov 10 at 18:10
I've used it in other programs and it worked. Do you have any suggestions? @Quimby
– Alice
Nov 10 at 18:10
@NeilButterworth I need to use chars to implement another function that counts the number of chars in the file
– Alice
Nov 10 at 18:11
Use stringstreams to read whole files. stackoverflow.com/questions/2602013/…
– Quimby
Nov 10 at 18:12