Question:
In C++, is there away to open up a .txt file with a sentence and store that sentence in an array?
MrGoodyear812
2010-05-04 08:23:44 UTC
Cause i havent a clue on how to write a program that does:

1. Reads the sentence from the file Text1.txt and display it on the screen.
2. Counts the number of characters of the sentence (including the whitespaces, but excluding the terminating NULL) and displays such information.
3. Replaces the original sentence in Text1.Txt with the reversed sentence (i.e “the cat is on the table” with “elbat eht no si tac eht”) and saves the updated file Text1.txt.
NOTE: Every file must be explicitly opened and then closed immediately after the read/write operations.

i was thinking if there was a way to save it as an array i could do it

or am i just super confused....

HELP!

thanks in advance
Four answers:
Cubbi
2010-05-04 09:04:24 UTC
Nothing in your description asks for an array. Why not use a string?



If you must use an array to read the string, it's



#include

#include

using namespace std;

int main()

{

const size_t size = 100; // or some other large number, but if the sentence is longer than that, it won't be read in its entirety. Which is why strings are better.

char array[size];

ifstream f("Text1.txt");

f.getline(array, size); // this will read up to end of line or size-1 characters read, whichever comes first. If your sentence is multiline, use f.read(array,size);

cout << array << endl; // to test what you've read

}
Chris C
2010-05-04 08:47:30 UTC
The first poster answered your question accurately. Yes, is the answer.



How to do it is another thing. This is apparently homework, so I'll suggest a few functions that you need to research in order to accomplish that task:

File operations you could use (there are others, these will just work well with your problem):

- fopen()

- fgets()

- fclose()



Standard C/C++ items to use:

- Loop (either use "for ()", or "while()" loop)

- Incrementing (counter increment is done like this: myIntVariable++)

- Array of characters is defined like this (without the quotes): "char mySentence[512];"
?
2010-05-07 13:58:49 UTC
Just check this out, please:



#include

#include

#include

using namespace std;

void rev_string()

{

// read file content into a vector of strings

ifstream f("text.dat");

string str;

vector vs;

if (f.bad()) {

cout << "couldn't open text.dat!" << endl;

return;

}

while(!f.eof()) {

str = "";

getline(f, str);

vs.push_back(str);

}

f.close();



// Displaying strings and count reverted/unreverted

for (unsigned i = 0; i < vs.size();) {

if (!vs[i].empty()) {

cout << " Display string: " <
<< " count: " << vs[i].length() << endl

<< "Display reverted string: ";

reverse(vs[i].begin(), vs[i].end());

cout << vs[i] << endl;

++i;

}

// Delete empty lines (this would usually be the last line!

else{

vs.erase(vs.begin() + i);

}

}

// open text.dat for output

ofstream of("text.dat");

// storing all sentences into file

copy(vs.begin(), vs.end(), ostream_iterator(of, "\n"));

// close file

of.close();

}
Neunerball
2010-05-04 08:32:23 UTC
Yes.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...