I have an existing text file called database.txt. If I try to add a string at the end that contains a space, it only adds the string content until the space. How can I add the whole string input, including the spaces?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
fstream database;
database.open("Database.txt",ios::app);
cout<<"Name";
cin>>line;
database<<line;
database.close();
return 0;
}
Instead of using std::cin >> line; try use std::getline(std::cin,line);
Related
I would like to read the content from the text file by C++ , I wrote the code as below:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream input("C:\\Users\\thang\\Desktop\\Thang\\output.txt");
string str;
while(!input.eof()) //check if it goes to the end of the file
{
//getline(input,str); //
input>>str; // get the value of str from the file
cout<<str<<endl;
}
return 0;
}
But it just show me the result with no empty row , but the output.txt file has the empty line.
I have attached the screenshot
Could you please help explain why it get the result without the empty line ? Appriciated for all assist.
Because the operator>> to read std::string from std::ifstream skips leading whitespaces. Newline characters is one kind of whitespace characters, so they are ignored.
Because input>>str strips away any leading whitespace.
If you just need to read the file like cat does, use rdbuf directly:
ifstream input(...);
std::cout<<input.rdbuf()<<std::endl;
If you want to iterate over the lines, including the empty ones, use std::getline
ifstream input(...);
for (std::string line; std::getline(input, line); )
{
...
}
When I compile my code all is good no errors but when I open the file that I am writing too I see that most of the text has been converted into strange Chinese looking characters.
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string temp, line;
fstream file("LogData.csv");
fstream txtfile("temp.txt", ofstream::out, ofstream::trunc);
while (getline(file, line, '\n'))
{
txtfile << line;
}
file.close();
txtfile.close();
}
I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween
I'm having some problems reading a string into an array. my file contains the following strings running horizontally down the page.
File:
dog
cat
rabbit
mouse
Code:
#include <string>
int i = 0;
using namespace std;
int main()
{
FILE * input1;
fopen_s(&input1, "C:\\Desktop\\test.dat", "r");
string test_string;
while (!feof(input1)) {
fscanf_s(input1, "%s", test_string);
i++;
}
return 0;
}
Any advice would be appreciated, Thanks!
You should use ifstream and std::getline
Now, I'm going to walk you through reading lines from the file using ifstream
Include fstream to use ifstream.
#include <fstream>
Opening a file:
To open a file, create an object of ifstream, and call it's method open and pass the filename as parameter. ifstream opens a file to read from it. (To write in a file, you can use ofstream)
ifstream fin;
fin.open("C:\\Desktop\\test.dat");
Or you can simply pass the filename to the constructor to create an object of ifstream and open a file.
ifstream fin("C:\\Desktop\\test.dat");
Reading from the file:
You can use stream extraction operator (>>) to read from the file, just like you use cin
int a;
fin >> a;
To read a line from a file using the above created fin (using a char array)
char arr[100];
fin.getline(arr, 100);
Better yet, you should use std::string instead or char arrays, using std::string, you can read a line using std::getline
string testString;
getline(fin, testString);
Now, let's change your code to use ifstream and getline
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int i = 0;
ifstream input1;
input1.open("C:\\Desktop\\test.dat");
string test_string;
while (getline(input1, test_string)) {
i++;
}
return 0;
}
I read line by line of a textfile and if a line meets some requirements I want to override the line and save it at the same position in the same file.
Here is what I have (simplified):
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
fstream file;
string line;
file.open("Test.txt");
while (getline(file, line))
{
if (line.size() > 7) file << line.append(" <- long line");
}
}
You can read your file into memory and then write it out after changing any of the lines. The following example reads it into a vector, then writes it back out.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file;
string line;
file.open("Test.txt", fstream::in);
if (file.fail()) exit(-1);
vector<string> vec;
while (getline(file, line, '\n'))
{
string ln = line;
vec.push_back(ln);
}
file.close();
// manipulate your lines here
file.open("Test.txt", fstream::out | fstream::trunc);
for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
file.write(it->c_str(), it->length());
file.write("\n", 1);
}
file.close();
}
But note, when you change a line, the position of lines that follow will change unless the line you are changing is the same size as the original. Also note, this is a simple ANSI file example, but UNICODE and UTF-8 are also common text file formats. This should at least get you started.