I am attempting to read from a .csv file in c++. After calling myfile.open(file), is_open returns true, but getline is only returning empty strings.
I have attempted using vectors to read the lines and then writing the vector data to a variable to read, but that has also returned only empty strings.
std::ifstream csvFile;
std::string line = "!", temp= "...";
csvFile.open("file.csv");
if(csvFile.is_open()) {
std::cout << "open\n";
std::cout << line << "\n";
if(getline(csvFile, line)) {
std:: cout << line << "\n";
} else {
std::cout << temp << "\n";
}
}
else {
std:: cout << "not opening\n";
}
std:: cout << line;
My output is as follows after running.
[ctest] open
[ctest] !
[ctest] ...
[ctest]
As shown, the getline() return only an empty string, although the file itself is not empty.
These are the first 20 or so lines of the csv file, and I have made sure that the file is in the current working directory.
Alpha002
16:55:54 13/6/2019
428,1.61,-1.31,-0.13,0,0
448,1.61,-1.47,-0.13,0,0
468,1.68,-1.07,-0.44,0,0
488,1.61,-1.39,-0.76,0,0
508,1.61,-1.47,-0.68,0,0
3528,1.61,-1.55,-0.36,0,0
3548,1.61,-1.31,-0.28,0,0
3568,1.68,-1.15,-0.36,0,0
3588,1.68,-1.63,-0.76,0,0
3608,1.68,-0.76,-0.68,0,0
3628,1.68,-1.15,-0.21,0,0
3648,1.68,-0.76,-0.28,0,0
3668,1.68,-1.39,-0.13,0,0
3688,1.68,-1.07,-0.21,0,0
3708,1.61,-1.47,0.03,0,0
I am not sure how to proceed from here, as i cannot find any issues apart from that, any advice is appreciated!
I found the issue, something is wrong with the file itself I was using. I tried reading from another file using the same methods, and it worked without issues. Thank you to everyone who commented trying to help!
I am trying to load a sample text file with 8488 characters (including spaces) so I can then organise the words in the text file into alphabetical order in a GUI (To create a dictionary essentially).
The .txt file loads the text successfully however I can not read spaces from that file, it just shows all of the words with no spaces between
I have a class dictionary and within dictionary.cpp source file I attempt to load and read the text file as shown below.
void dictionary::loadFile(const char *fileName)
{
char value;
ifstream f_in(fileName);
if (!f_in)
{
cerr << "\nError loading file!" << endl;
exit(1);
}
else
{
cout << "File loaded successfully\n" << endl;
}
for (int i = 0; i < 8488; i++)
{
f_in >> value;
Memory[i] = value;
cout << Memory[i];
}
}
Can someone explain where I may have went wrong?
I'm quite new to working with classes, particularly in QT creator.
Issue has been fixed.
I used
f_in >> noskipws;
to acknowledge the white spaces.
Edit: changed my question to be more accurate of the situation
I'm trying to open up a text file (create it if it doesnt exist,open it if it doesnt). It is the same input file as output.
ofstream oFile("goalsFile.txt");
fstream iFile("goalsFile.txt");
string goalsText;
string tempBuffer;
//int fileLength = 0;
bool empty = false;
if (oFile.is_open())
{
if (iFile.is_open())
{
iFile >> tempBuffer;
iFile.seekg(0, iFile.end);
size_t fileLength = iFile.tellg();
iFile.seekg(0, iFile.beg);
if (fileLength == 0)
{
cout << "Set a new goal\n" << "Goal Name:"; //if I end debugging her the file ends up being empty
getline(cin, goalSet);
oFile << goalSet;
oFile << ";";
cout << endl;
cout << "Goal Cost:";
getline(cin, tempBuffer);
goalCost = stoi(tempBuffer);
oFile << goalCost;
cout << endl;
}
}
}
Couple of issues. For one, if the file exist and has text within it, it still goes into the if loop that would normally ask me to set a new goal. I can't seem to figure out what's happening here.
The problem is simply that you are using buffered IO streams. Despite the fact that they reference the same file underneath, they have completely separate buffers.
// open the file for writing and erase existing contents.
std::ostream out(filename);
// open the now empty file for reading.
std::istream in(filename);
// write to out's buffer
out << "hello";
At this point, "hello" may not have been written to disk, the only guarantee is that it's in the output buffer of out. To force it to be written to disk you could use
out << std::endl; // new line + flush
out << std::flush; // just a flush
that means that we've committed our output to disk, but the input buffer is still untouched at this point, and so the file still appears to be empty.
In order for your input file to see what you've written to the output file, you'd need to use sync.
#include <iostream>
#include <fstream>
#include <string>
static const char* filename = "testfile.txt";
int main()
{
std::string hello;
{
std::ofstream out(filename);
std::ifstream in(filename);
out << "hello\n";
in >> hello;
std::cout << "unsync'd read got '" << hello << "'\n";
}
{
std::ofstream out(filename);
std::ifstream in(filename);
out << "hello\n";
out << std::flush;
in.sync();
in >> hello;
std::cout << "sync'd read got '" << hello << "'\n";
}
}
The next problem you'll run into trying to do this with buffered streams is the need to clear() the eof bit on the input stream every time more data is written to the file...
Try Boost::FileSystem::is_empty which test if your file is empty. I read somewhere that using fstream's is not a good way to test empty files.
I have a text file that contains several words, all separated by spaces. I'm trying to read the file and then put it into an array, so that each word is a separate value in said array. I'm using this code, but when I run my program, it doesn't display anything (like it should.)
ifstream file ("words.txt");
if(file.is_open())
{
string wordArray[100];
for(int i = 0; i < 100; ++i)
{
file >> wordArray[i];
cout << i;
}
cout << "File is open.";
}
Nothing displays at all. I'm doing this in a void function, which isn't being passed anything currently, but I don't think that has anything to do with it. The code should at least display "File is open" or any number from 1 to 100, but I don't get anything. I don't understand why this isn't working, as I'm including iostream, string, fstream, iomanip, and sstream. If there's something simple I'm overlooking, please let me know.
Well, your program probably isn't passing the statement in the if condition.
Try adding this to test your file is opening correctly:
if(file){
// do all the file inputs
}
else{
std::cerr << "could not open file words.txt" << std::endl;
}
I need to know if you can easily get the number of data entries in another file and save that number in the original file. Need a program that will process the other file no matter how many entries are in it. Hope that makes any sense.
Your question is very poorly worded but I think you are looking for getline. This function can parse an input file based on the newline character (default behaviour) or based on a user provided delimiter:
int entryCount = 0;
std::string currentLine;
std::ifstream inFile( "in.txt" );
std::ofstream outFile;
if (inFile) // short for inFile.good())
{
while (std::getline( inFile, currentLine))
{
++entryCount;
// Do your processing
}
inFile.close();
outFile.open( "out.txt" );
outFile << "End of file. " << entryCount << " entries read." << std::endl;
outFile.close();
}
else
std::cout << "oops... error opening inFile" << std::endl;