Ofstream adds blank line to end of file - c++

Basic question I'm hoping theres a way to solve. When I write to a file in my c++ with ofstream it adds a blank line at the end. Is there anyway to stop this behavior? It is causing issues when I load the file the next time the program is run.
int saveScores(scoreboard sBoard, const string filename) {
ofstream outStream;
outStream.open("/Users/Wescat/Desktop/Programming/comp2710/wsc0010_project1/scores.txt");
if (outStream.fail()) {
cout << "Output file opening failed." << endl;
return 1;
}
int i = 0;
while (i < sBoard.numOfScores) {
outStream << sBoard.score->name << endl;
outStream << sBoard.score->score << endl;
sBoard.score = sBoard.score->next;
i++;
}
outStream.close();
return 0;
}

This is perfectly reasonable.
What you're interpreting as a blank line is just the way your text editor represents the presence of a \n character at the end of your actual final line (generated by your final std::endl). It makes sense for that character to be there, as it is as the end of all of your lines. Indeed, this is what "line ending" means!
If this is causing problems when you load the file back in, then I'd suggest that your real bug is in fact there.

Related

How to read back what I just wrote to a file?

I've been trying to write a program to open a file in both read and write mode:
#include <fstream>
#include <iostream>
using namespace std;
int main(){
fstream obj;
obj.open("hello.txt",ios::in|ios::out);
if (!obj){
cout << "File not opened" <<endl;
return 1;
}
obj << "Hi How are you" ;
char c;
while (!obj.eof()){
obj.get(c);
cout << c;
}
obj.close();
return 0;
}
When I compile this program on Visual Studio Code on Windows, though the text "Hi how are you" is printed in the file, the contents of the file are not printed on my screen. Can someone tell me what might be the problem?
Resetting the position indicator with seekp to 0 helps, because both output and input indicators are set to the end of file after write operation (you can read them with tellp tellg).
obj << "Hi How are you" ;
obj.seekp(0);
char c;
while (!obj.eof()){
obj.get(c);
cout << c;
}
Considering avoiding using obj.eof(), you can e.g. read your file line by line:
std::string line;
std::getline(obj, line);
std::cout << line << std::endl;
or in the loop:
while (std::getline(obj, line)) // here std::basic_ios<CharT,Traits>::operator bool is used to check if operation succeeded
{
std::cout << line << std::endl;
}
You got two problems there: buffering and seek position.
Buffering:
When you write the text with obj << "Hi How are you, you just write it into the buffer and the text gets written into the file after flushing the buffer. You can adjust which buffer type you want to use. The easiest way is to write std::endl after your text if you use line buffering.
A better explaination is already here
Seek Position:
You are reading from the last position in your file. You have to manually change the read position to the first character in the file, then you are done.

is_open returns true, but getline only returns empty strings

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 Can't print out a file that I wrote on

I have created a function to write some data on a text file, and it works fine. I created another function to read in all the content of the file, and print it out for me! But, it is not working for some reason. Could any one help please?
This is my function:
void myClass::displayFile() {
char line[LINE]; //to hold the current line
file.open("data.txt", ios::app);
//keep reading information from the file while the file is open and has data
while (!file.fail() && !file.eof()) {
int lineSize; //to loope through each line
file.getline(line, LINE);
lineSize = strlen(line);
//loop through the line to print it without delimiters
for (int i = 0; i < lineSize; ++i) {
if (line[i] == ';') {
cout << " || ";
} else {
cout << line[i];
}
}
}
file.close();
file.clear();
if (file.fail()) {
cerr << "Something went wrong with the file!";
}
}
Note: The function compiles and the loop is accessible, but the line string is empty.
This is the writing function:
void myClass::fileWriter() {
file.open("data.txt", ios::app);
file << name << ";" << age << ";" << "\n";
file.close();
file.clear();
}
Silly me, the cause of your problem was staring me right in the face from the beginning, and it's the app open-mode that's the problem. It is to open the file in write mode, which means you can't read from it.
And even if you could read from the file, the cursor is placed ad the end of the file the eofbit flag would have been set inside the first iteration anyway.
If you want to read from a file, then either use std::ifstream which automatically sets the in mode if you don't specify a mode, or you have to explicitly set the in mode when opening.

Reading an text file into an array of strings

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

remove stop words from a text file

I'm still a beginner to programming, I got an assignment asks me to make a code that read a text file and then remove the stop words. I made a code but it's not that good. what i want is to know how to remove a word from a line and apply case folding to the file after removing the stop words.
here is my code...
string line, deleteline;
ifstream stopword;
stopword.open("example.txt");
if (stopword.is_open())
{
while ( getline (stopword,line) )
{
cout << line << endl;
}
stopword.close();
}
else cout << "Unable to open file";
ofstream temp;
temp.open("temp.txt");
cout << "Please input the stop-word you want to delete..\n ";
cin >> deleteline;
while (getline(stopword,line))
{
if (line != deleteline)
{
temp << line << endl;
}
}
temp.close();
stopword.close();
remove("example.txt");
rename("temp.txt","example.txt");
cout <<endl<<endl<<endl;
system("pause");
return 0;
}
The code would be right, but you closed the file... and after closing, you're trying to read from it later. That won't quite work, unless you open it again.
Here you close the file (7th line in main):
stopword.close();
And then, in the 2nd while you try to getline from that stream:
while (getline(stopword,line))
In order for that to work, you have to open the file again. And clear the stream, because it'll probably have the eofbit error state flag set.