First, I'm sorry but I don't speak english very well.
My problem is that I want my stream go back begining of the file. So, i apply the clear() method upon my stream object, but after this, getline() always return 0 (false).
I don't find the solution. Somebody has an idea about this problem?
So, this is my code:
void Tools::tokenizeAll(string filename, string separator){
char str[LINESIZE] = {0};
int lineNumber = 0, j = 0;
ifstream stream;
stream.open(filename.c_str(), std::ifstream::in);
if(stream){
while(stream.getline(str, LINESIZE)){
lineNumber++;
}
//allocation dynamique du tableau à deux dimensions
string** elementsTable = NULL;
elementsTable = new string*[lineNumber];
for( int i = 0 ; i < lineNumber ; i++ ) elementsTable[i] = new string[4];
std::cout << " good()=" << stream.good() << endl;
std::cout << " eof()=" << stream.eof() << endl;
std::cout << " fail()=" << stream.fail() << endl;
std::cout << " bad()=" << stream.bad() << endl;
cout << endl;
stream.clear();
std::cout << " good()=" << stream.good() << endl;
std::cout << " eof()=" << stream.eof() << endl;
std::cout << " fail()=" << stream.fail() << endl;
std::cout << " bad()=" << stream.bad() << endl;
cout << endl;
cout << stream.getline(str, LINESIZE) << endl;//return 0
}
else cout << "ERREUR: Impossible d'ouvrir le fichier en lecture." << endl;
}
Thank you very much (and thank you to warn me about my english errors ;) )
Calling clear() only resets the error flags. To "rewind" the stream to the beginning, you'll also need to use seekg:
stream.seekg(0, std::ios::beg)
Note that this operation can fail, too, so you might want to check for errors.
Related
I'm basically trying to add integers to a string, and then add endline chars to the string at specific times, and then continue adding integers. I want it all added to just one string object.
So I added an int to stringstream, fed stringstream to a string. Then added '\n' to the string.
Then I added another int to stringstream, and fed it to the string again.
Unfortunately, the second object from the stringstream object is not being added to the string, and I'm not sure why it cannot.
I tried adding two objects to my string stream object, and then feeding those to the string one at a time (with the endline character in between), but stringstream adds everything in the string to the string at once, so that doesn't work.
int main(){
string h;
stringstream ss;
ss << 1;
ss >> h;
h += '\n';
ss << 2;
ss >> h;
h += '\n';
cout << h << "endline";
cout << endl;
return 0;
}
There are no error messages of course. I expect it to output:
1
2
endline
Instead I get
1
endline
So obviously the string is adding both of my endline characters, but not the 2 that I added to the stringstream.
To demonstrate what happened we need to inspect the various stream status bits.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1)
{
cout << "1 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "2 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
h += '\n';
if (ss << 2)
{
cout << "3 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "4 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
h += '\n';
cout << h << "endline";
cout << endl;
}
else
{
cout << "4 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "3 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "2 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "1 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
Output
1 success. Fail 0 eof 0 bad 0
2 success. Fail 0 eof 1 bad 0
3 Fail 1 eof 1 bad 0
So the first write succeeded. Flawless victory. But the first read read everything in the stream and hit the end of file (not that the stream is a file, but the name stuck), setting the EOF bit. Once the EOF bit is set, there isn't much you can do with a stream other than clear the bit and pray someone adds more data to be read.
More data was added to the stream, but the file could not accept it because of the EOF bit.
If we clear the EOF
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1)
{
cout << "1 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "2 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
h += '\n';
ss.clear(); // added this
if (ss << 2)
{
cout << "3 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "4 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
h += '\n';
cout << h << "endline";
cout << endl;
}
else
{
cout << "4 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "3 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "2 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "1 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
the output is now
1 success. Fail 0 eof 0 bad 0
2 success. Fail 0 eof 1 bad 0
3 success. Fail 0 eof 0 bad 0
4 success. Fail 0 eof 1 bad 0
2
endline
If we ignore all the status information I added we really got
2
endline
not the desired
1
2
endline
because ss >> h will overwrite everything already in h. The "1\n" is wiped out by the "2"
The easiest way to get what you want is to write everything in and then get the contents as a string.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1 << '\n' << 2 << '\n')
{
cout << ss.str() << "endline";
cout << endl;
}
else
{
cout << "Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
This time the output is
1
2
endline
Use the stringstream to buildup the output you want, and then extract the string.
ss << 1 << '\n';
ss << 2 << '\n';
h = ss.str();
I have just more or less finished my first C++ Project, it is a Hangman Game and so far everything works fine. The only Problem is that i need to have spaces between the underlines (_) that represent the hidden word. If anyone could help me on this i would really appreciate it.
// UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS
#include "tests.h"
#include <iostream>
#include <string>
#include "hangman.h"
int main(){
using namespace std;
// display the hidden word
std::string word_to_guess = chooseWord();
int misses = 0;
std::string displayed_word = word_to_guess;
for(int i=0; i< displayed_word.length(); i++)
displayed_word[i] = '_';
int attempts = 6;
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for correct letter
while(1){
std::cout << "Your guess";
std::cout << ":";
char guess;
std::cin >> guess;
bool Correct = false;
for(int i=0; i< word_to_guess.length(); i++)
if (guess == word_to_guess[i]) {
displayed_word[i] = word_to_guess[i];
Correct = true;
}
if (!Correct)
attempts--;
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for win or lose
if (attempts==0)
std::cout << "The word was: " << word_to_guess << std::endl << "You lost!";
if (attempts==0)
return 0;
if (!word_to_guess.find(displayed_word))
std::cout << "You won!";
if (!word_to_guess.find(displayed_word))
return 0;
}
}
First, you can simplify this
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
Now, about your question, I think a best solution is replacing
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
if(i == 0 || displayed_word[i] == '_')
std::cout << " ";
std::cout << displayed_word[i];
if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
std::cout << " ";
}
std::cout << "]" << std::endl;
Explaination:
We put spaces at the beginning and the end, and also around underscores, but we make sure to put only one space between two underscores.
I need help getting declared string function to change white space of input file to a specific character.
if (infile.fail())
{
cout << "The file doesn't exist";
exit(-1);
}
else
{
numBooks = readFile (infile, magSub, 260);
for (i=0; i<numBooks; i++)
{
cout << "Last Name: " << magSub[i].lastName << endl;
cout << "First Name: " << magSub[i].firstName << endl;
cout << "Street Address: " << magSub[i].address << endl;
cout << "City: " << magSub[i].city << endl;
cout << "State or Province: " << magSub[i].state << endl;
cout << "Country: " << magSub[i].country << endl << endl;
cout << "Zip or Postal Code: " << magSub[i].zip << endl;
cout << "Expiration Date: " << magSub[i].expDate << endl;
cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;
}
writeFile(outfile, magSub, numBooks);
}
}
void fillSpace (string &expDate)
{
for (int i=0; expDate.length(); i++)
{
if (isspace(expDate[i]))
expDate[i] = '0';
}
}
I have the function declared above main. I know I need to call the function but I can't get it to change the white spaces.
In your code for fillSpace, you are not checking for the end of string condition. You should use i<expDate.length() for checking the end of string.
You have missed the check condition in for loop of fillSpace function.
for (int i=0; i < expDate.length(); i++)
And for calling the function
you have to declare a string which will store the string from the magSub[i].expDate.
and then pass that string to the function fillSpace.
After that you will get the string with replaced char space with '0'.
cout << "Expiration Date: " << magSub[i].expDate << endl;
please use the following code:
string temp = magSub[i].expDate; // copy the string to the temp string/char array
fillSpace (temp); // Missing Line for function call
cout << "Expiration Date: " << temp << endl; // replace line with
Hope
this will Help you.
I have a class that's supposed to write to a gml file defined below. The class has one method that does the writing. If I call the function, I get a core dump when the main function returns. I can create objects of the class with no problem, it only happens when the write function is called. The function also returns with no error and the rest of the program runs.
GML Writer:
class GMLWriter {
public:
void write(List<User*> usr, const char* filename);
};
void GMLWriter::write(List<User*> usr, const char* filename)
{
cout << "Filename: " << filename << endl;
ofstream outfile;
outfile.open(filename);
if (!outfile.is_open())
cout << "Couldn't open the file..." << endl;
outfile << "graph [\n";
// Write user data
for (int n = 0; n < usr.size(); n++) {
cout << "Writing node..." << endl;
outfile << "node [\n";
outfile << "id " << usr[n]->getID() << "\n";
outfile << "name \"" << usr[n]->getName() << "\"\n";
outfile << "age " << usr[n]->getAge() << "\n";
outfile << "zip " << usr[n]->getZip() << "\n";
outfile << "]\n";
}
// Write associations
for (int n = 0; n < usr.size(); n++) {
List<int> tList = usr[n]->getFriends();
cout << "Writing edge..." << endl;
//List<int> tempL = usr[n]->getFriends();
for (int i = 0; i < tList.size(); i++) {
outfile << "edge [\n";
outfile << "source " << usr[n]->getID() << "\n";
outfile << "target " << tList[i] << "\n";
outfile << "]\n";
}
}
outfile << "]"; // end graph
cout << "End function" << endl;
outfile.close();
}
User simply contains the variables to write to the file, and those methods work fine.
I've spent hours with this in a debugger and haven't been able to find the problem. Any help would be greatly appreciated.
Thanks!
Try looking at the core dump: http://www.network-theory.co.uk/docs/gccintro/gccintro_38.html
This seems like it should be easy, which is why it is driving me especially insane. Hopefully someone out there will see the problem right off. I'm just trying to build arrays from an array built from a user input. It seems to create an array that is bigger than the one I meant for it to. Here's the program:
int main()
{
ifstream inFile;
ofstream outFile;
int numReq, fileSize;
string lang, dash;
char fileName[40];
char confirm[10];
char confirm2[10];
int character;
char year[3];
char month[1];
char day[1];
char hour[1];
cout << "What file to process?" << endl;
cin >> fileName;
year[0] = fileName[14];
year[1] = fileName[15];
year[2] = fileName[16];
year[3] = fileName[17];
cout << "year = " << year << "." << endl;
month[0] = fileName[18];
month[1] = fileName[19];
cout << "month = " << month << "." << endl;
cout << "so I gotta know, what is..." << endl;
cout << "month[0]? " << month[0] << endl;
cout << "month[1]? " << month[1] << endl;
cout << "month[2]? " << month[2] << endl;
cout << "month[3]? " << month[3] << endl;
cout << "month[4]? " << month[4] << endl;
cout << "month[5]? " << month[5] << endl;
cout << "month[6]? " << month[6] << endl;
day[0] = fileName[20];
day[1] = fileName[21];
cout << "day = " << day << "." << endl;
hour[0] = fileName[23];
hour[1] = fileName[24];
cout << "hour = " << hour << "." << endl;
cout << "so, 'fileName[23]' is = " << fileName[23] << "?" << endl;
cin >> confirm;
cout << "So, the year is " << year << ", the month is " << month
<< ", the day is " << day << ", the hour is " << hour << "?" << endl;
cin >> confirm;
//cout << "Is this what you chose? " << fileName << endl;
//cin >> confirm;
//cout << "Which character to manipulate?" << endl;
//cin >> character;
//cout << "This one? " << fileName[character] << endl;
//cin >> confirm2;
inFile.open(fileName);
assert (!inFile.fail());
outFile.open("revisedPracticeFile1.txt");
outFile << fixed << showpoint; // I have no idea what this is...
outFile << setprecision(2); // .. or this for that matter.
cout << "Processing data" << endl;
inFile >> lang;
while (!inFile.eof() ){
if (lang.length() <= 2){
outFile << lang << " ";
// I should keep in mind, that, for whatever reason, it seemed like the
//item 'setw(6)' made the program work when I put it in, but didn't seem
//to make the program stop working when I took it out. Curious..
inFile >> dash >> numReq >> fileSize;
outFile << numReq << " " << fileSize << endl;
}
else{
inFile >> dash >> numReq >> fileSize;
cout << "took out " << lang << " " << numReq << " " << fileSize << endl;
}
inFile >> lang;
}
inFile.close();
//assert(!inFile.fail());
outFile.close();
return 0;
}
...And, this is what happens when I run the program:
What file to process?
projectcounts-20090101-010000
year = 2009.
month = 01009.
so I gotta know, what is...
month[0]? 0
month[1]? 1
month[2]? 0
month[3]? 0
month[4]? 9
month[5]?
month[6]?
day = 011009.
hour = 0111009.
so, 'fileName[23]' is = 0?
yes
So, the year is 1009, the month is 11009, the day is 111009, the hour is 0111009?
^C
... So what gives?
The syntax char year[3]; declares an array with 3 element. But, then you use it to store 4 elements. There are similar issues with your other arrays.
Also, you're using char arrays as strings. That's a C (not C++) way to do things. Of course you're allowed to do this if you want. But, these c-style strings use the convention that the last item is a zero.
Thus, if you wanted a C-style string to store the work 'foo', you could do it like this
char string[10]; // anything bigger than 3 works
string[0] = 'f';
string[1] = 'o';
string[2] = 'o';
string[3] = '\0'; // this zero tells functions like `printf` that the string has ended.
Without that last zero, functions like printf will just keep outputting memory locations until it happens upon a zero somewhere.
EDIT: Consider using c++ std::string for your string processing.