I have the endl, but its not going into my file, so when I enter in more than 1 line, its all on the same line in the notepad.
I've tried:
codeFile << codeLine;
codeFile << endl;
I've also tried adding a "\n" to the string by adding a constant string to it but it doesn't work.
//Writing Coded Lines to File:
if(codeFile)
{
//Relaying Feedback to User:
cout << "File has been successfully opened/created" << endl;
cout << "\nWriting to file..." << endl;
for(int i = 0; i < lines; i++)
{
//Getting Non-Coded Line from User:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.getline(line, length);
//Creating Copy of Line:
strcpy(cline, line);
//Gathering Line Length:
length = strlen(line);
//Coding Line:
codedLine = code(length, line, cline, sAlphabet);
//Coded Line Test
cout << codedLine;
//Writing to File:
codeFile << codedLine << endl;
}
}
//Closing File:
codeFile.close();
cout << "\nFile has now been closed";
}
Cygwin mocks a POSIX system and uses Unix line endings, not the Windows line endings understood by NotePad.
Replacing endl with '\n' won't help. endl is a '\n' followed by a stream flush.
The best option is to use a different file reader, WordPad for example, that understands Unix line endings. The alternatives are to
Change your compiler toolchain to one that is not emulating a POSIX operating system, or
Brute force a Windows line ending with \r\n, but this means your code will have a similar wrong line-ending problem on a Unix -based system.
Related
When I try to Find a Keyword from the GetLine() Function It only Returns a Empty String. But when I try to print the Line with std::cout, it prints everything, so i debugged the solution I found that the String is empty but std::cout somehow manages to print the lines in file.
std::ifstream file(fileToSearch);
std::string line;
cout << endl << keyWords[i];
while (std::getline(file, line)) {
cout << endl << keyWords[i]<<" ";
cout << line;
if (auto pos = line.find(keyWords[i]) != std::string::npos) {
// std::stringstream iss(line.substr(pos + keyWords[i].size()));
cout << endl << "found again in String Table" << endl;
//iss >> j;
break;
}
}
This is the data in the File
%Checking Windows(R) Installer VersionConfiguring Windows InstallerConfiguring %sĀ½Setup has completed configuring the Windows Installer on your system. The system needs to be restarted in order to continue with the installation. Please click Restart to reboot the system.%s
When I checked the Encoding of the file in Notepad++. I found that the file is not encoded at all , but when I changed the file to UTF-8 I can get the Value and Search It Successfully. Is there anyway to do this by code rather than changing it Manually??
I am trying to read the first line of a file.
cout << "File: " << endl;
string fileName;
cin >> fileName;
cout << fileName << endl;
ifstream infile(fileName);
string line;
getline(infile,line,'\n');
cout << line << endl;
I am using CodeBlocks and I have a file named "1.txt" in the same directory as main.cpp.
This file contains 2 lines of thext, but the line variable is always empty. What am I missing here?
So after a LOT of searching I finally realized that I used notepad instead of notepad++ and the file got named as 1.txt.txt
Leaving this answer if anyone has the same problem in the future.
This is my code here.
ifstream inFile;
ofstream outFile;
string line, line2;
inFile.open("DATA.txt");
outFile.open("DATA.txt");
getline(inFile, line);
cout << line;
getline(inFile, line2);
cout << line2;
getline(cin, line);
getline(cin, line2);
outFile << line << "\n" << line2;
From what I understand, getline(inFile, line) should assign the first line of my text file to a string named line. Then the cout << line should print that string into the cmd window. This is not working though.
I am able to input just fine using getline(cin, line) and outFile though. The file gets updated and I can see what I typed in it, but it just doesn't properly read and print the lines.
p.s This is my first question and I'm not entirely sure how to ask it in the title so I'm open to criticism.
On most platforms, std::cout typically buffers output data and does not flush to the console until a line break is output, or the buffer is flushed explicitly.
Try using:
cout << line << '\n';
Or:
cout << line << endl;
If you don't want to output line breaks, use:
cout << line << flush;
So I figured it out. When I had
inFile.open("DATA.txt");
outFile.open("DATA.txt");
I think the outFile.open overwrote the inFile.open, causing only the output part to work.
Simply moving the outFile.open to before the output part instead of before everything fixed this problem. I'm sure there are several other sloppy things about my code, but that fixed it to the point of actually working.
Note: I am using the C++11 standard, so I don't see why this isn't working with or without c_str() appended.
I have the following code:
// open streams
ifstream in(input);
ofstream out(output);
// get which file to open
in.ignore(INT_MAX, ':'); // we don't need the beginning part
in.ignore(); // remove trailing whitespace
string fileLocation;
getline(in, fileLocation);
out << "Loading: " << fileLocation << endl;
cout << "Loading: " << fileLocation << endl;
// now that we know where the file is, load it:
ifstream file(fileLocation);
which reads from a file that looks vaguely like this
File: file.txt
(Subcommands below)
I know that I am pulling the correct filename because of the terminal output.
Anyway, I noticed that the stream wasn't opening properly, so I added this conditional to check:
if ( !file )
{
cout << "File wasn't loaded properly." << endl;
}
And sure enough, I see that message when running the program.
My question is this: how come, when I hard-code the file location, e.g. ifstream file("file.txt") it opens up no problem? How do I get this working properly?
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.