So I have a Test folder inside my workspace in CodeLite and inside Test folder I have:
main.cpp
test.txt
The problem is whenever I try to read from test.txt, the compiler deletes the file content and writes "Debug/main.cpp.o" inside my test.txt file. For example, if my txt file contains the following text:
Abcd ef
And my code inside main.cpp:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string data;
ifstream infile;
infile.open("text.txt");
cout << "Reading from the file" << endl;
infile >> data;
return 0;
}
When I run my code the output should be:
Reading from file
Abcd
ef
But instead, the output is:
Reading from file
And now my test.txt contains:
Debug/main.cpp.o
I am also inserting what my folder contains:
I don't know why it does this. Can anyone help?
CodePudding user response:
This is happening because you haven't use/printed the data variable. Use this code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
string data;
ifstream infile;
infile.open("test.txt");
cout<<"Reading from file"<<endl;
while (getline(infile,data))
{
cout<<data<<endl;
}
return 0;
}
CodePudding user response:
Codelite generates $(project).txt
($(project)
is Test
in your case) with all objects filename for compilation (as response file (to bypass limit of command line length when there are too many files)).
Either place project in another directory or rename the file or project to avoid the conflict with that file.