ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin.getline(loadsentencetext, 50);
ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin >> loadsentencetext;
ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin.getline(loadsentencetext, 50);
ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin >> loadsentencetext;
std::string line;std::getline(your_stream,line);
ifstream fin ("conversation1.txt");char* getlinestring = new char[50];char* finstring = new char [50];fin.getline(getlinestring, 50);fin >> finstring;
Quote:Original post by CIJolly
My problem is how to load strings with spaces from the txt file.
Quote:Original post by CIJolly
By program jams, I meant I got an illegal operation error.
I wrote another variation, that looks like this:
*** Source Snippet Removed ***
And this also works fine. However, if I remove the line fin>>finstring, the program has an illegal operation error. If I remove fin.getline, the program continues to work.
If I output finstring, I get the word from the text file. If I output getlinestring, I get nothing.
So, the reason it was jamming was because getline wasn't doing anything at all, and wasn't advancing the place the program was reading the text file from to the next line (the next few lines contain ints, so the program would hit getline, do nothing, then start trying to read ints from a string).
I made a console app where getline works fine. However, in that program, I used the include
#include <fstream.h>
In this more advanced program I am having trouble with at the moment, I am using
#include <fstream>
If I try to use
#include <fstream.h>
with the more advanced program I am currently working on, I get these errors:
:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2149) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2216) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2822) : error C2872: 'ofstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2859) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2926) : error C2872: 'ofstream' : ambiguous symbol
So, for some reason getline does nothing, but it does in my more simple console program, but I have to use a different header in the simpler program.
If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...
#include <algorithm> // for std::copy#include <iostream> // for std::cout#include <iterator> // for std::ostream_iterator#include <fstream> // for std::ifstream#include <sstream> // for std::stringstream#include <string> // for std::string#include <vector> // for std::vectorint main(){ // Store all the strings from the file into a vector. std::vector<std::string> strings; // Initially allow space for 50 strings. const size_t STRING_COUNT = 50; strings.reserve(STRING_COUNT); std::ifstream fin("conversation1.txt"); // Make sure the file is actually open. if (fin.is_open()) { std::string line; // Keep reading lines until we reach the end of the file. while (getline(fin, line)) { // Tokenize the current line into strings. std::string temp; std::stringstream ss(line); // Extract the strings while (ss >> temp) { strings.push_back(temp); } // Clear the stringstream for more input. ss.str(""); } } // Print out the strings we have. std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(std::cout, " ")); // strings[0] = the first string // strings[1] = the second string // etc.}
Quote:Original post by ToohrVykWhy does your vector of strings start with STRING_COUNT empty strings?
Quote: std::vector<std::string> strings(STRING_COUNT);
Quote:Original post by CIJolly
By program jams, I meant I got an illegal operation error.
Quote:I wrote another variation,
Quote:So, the reason it was jamming was because getline wasn't doing anything at all,
Quote:
If I try to use
#include <fstream.h>
with the more advanced program I am currently working on, I get these errors:
Quote:If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...
Quote:Original post by CIJolly
Thanks for all the help, I made the changes you suggested, but I'm still having the same problem.
Quote:Original post by MaulingMonkey
At this point I'm going to ask for full program source (and sample text file while you're at it), because, well, Works Here™.Quote:If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...
Changing things at random to try and fix problems is just about the worst thing you can do in programming, as it will only make things worse in general. Figure out the problem, figure out the root cause (we're not there yet, only a string of disjoint symptoms so far...), and fix it.