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);










share|improve this question























  • 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














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);










share|improve this question























  • 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












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);










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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];






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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];






share|improve this answer






















  • 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














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];






share|improve this answer






















  • 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












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];






share|improve this answer














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];







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto