I am a beginner at file handling and I tried to create a file to store some data into a file in text format. But the expected text file is not created in the location I tried to create and when I search for the expected file using the name, it shows that there is no such file. So the file has not been created.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream firstFile;
firstFile.open("testFile.txt", ios::out);
if (firstFile.is_open())
{
firstFile << "My name is ABCD & this is my first text file in cpp";
firstFile.close();
}
cout << "This code is working properly";
return 0;
}
Related
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?
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.
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;
}
I am using visual studio 2017
I am new to c++ and here I tried to open a txt file, and confirming that it was opened.
#include <iostream>
#include <fstream>
#include <String>
using namespace std;
int main()
{
ifstream infile;
string text;
infile.open("C:\\Users\\gab_a\\source\\repos\\one\\testing.txt");
if (!infile.is_open()) {
cerr << "Specified file could not be found ";
exit(1);
}
else {
cout << "Opened file ";
infile >> text;
cout << text;
}
return 0;
}
it says that it opened it, and it even read the text that was inside the file, but the actual file isn't opening, I even put the file in the same directory as the project. There are also no errors, so why isn't my file opening?
What you're doing is reading the data from the file into a stream. This is not the same as executing a program to open the file. To do that is generally OS specific, but if you're on Windows you can use ShellExecute or CreateProcess. I do suggest you brush up on your C++ a bit - no offense intended
I am working on a text processor that takes in text from a file and inserts it into a Graph data structure. I made the Graph, but I am having trouble with the text processor. Whenever I execute the code, it says I am unable to open the file. I made sure that the text file was in the same directory when I executed the code. Here is the code for the GraphTextProcessor class:
#include <fstream>
#include <cstring>
#include <string>
#include "Graph.h"
class GraphTextProcessor {
private:
Graph* m_data;
public:
GraphTextProcessor();
Graph* process(std::string filename);
};
GraphTextProcessor::GraphTextProcessor() {
}
Graph* GraphTextProcessor::process(std::string filename) {
//process text file and insert into graph here
std::string word;
//opens file in read mode
std::ifstream readFile;
readFile.open(filename.c_str(), std::ios::in);
if (readFile.is_open()) { //Not opening
while (readFile >> word) {
std::cout << word << std::endl;
}
// Closes open text file
readFile.close();
}
else {
std::cout << "Unable to open text file." << std::endl;
}
return NULL;
}
I am just trying to read from a file first before I actually try writing to the Graph. Here is the code that I am running in Main:
#include <iostream>
#include <string>
#include "GraphTextProcessor.h"
int main() {
GraphTextProcessor *gp = new GraphTextProcessor();
gp->process("hello.txt");
}
It prints "Unable to open text file". Any suggestions?
I ran the code myself and it is working fine.
List your programming environment and what steps did you follow.Please make your question more elaborate and explain exactly what have you tried for making it work.
Please make sure the following:
Try using the full path name; for example,
ifstream in("C:/someDirectory/andSomeOtherDirectory/one.txt");
Try changing the spelling of the file.
For example:
"One.txt"
or
"ONE.txt"
You need permission to read the file. Try changing the permission of file.
Try different compilers
Also if you use exception handling (try, throw, catch) instead of if, else that will help finding the error.
I'm reading in a file name from the keyboard and opening the specified file. However it is not meeting my if statement that determines if it's open or not. Heres my code:
#include "prog.hh"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string fileName;
cout << "Enter the file name to be read: ";
cin >> fileName;
ifstream input_file("fileName");
std::string line_; // string which text file contents will be stored in
if(input_file.is_open()){ // validation to see if the file is open
while(getline(input_file, line_)){
std::cout<<line_<< '\n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
std::cout<<"File is not open"<< '\n';
}
std::cin.get();
After being compiled, I type in the file name I want to open and I get back the else message "File is not open", although the ifstream should of opened it. I definitely have the right file I'm trying to open in the correct folder. Any help appreciated, thanks.
change
ifstream input_file("fileName");
to
ifstream input_file(fileName);
When I debug this I can see it opens datafile1 , it reads the firstline and
in the logfile I get roma-3-4.log
It change to c:/temp/roma-3-4.log but when I want to open it , it fails. I have check that the _Mystate = 2 .
What is the meaning of that
Thanks
in the transfersubs.cfg there is this
roma-3-4.log
** In the directory c:/temp/ I have the following file
roma-3-4.log
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
string logfile;
string errorfile;
short logfilesize1;
fstream dataFile1("c:/temp/transfersubs.cfg", ios::in);
if (dataFile1)
{
getline(dataFile1, input, '$');
logfile=input;
logfilesize1=input.size();
errorfile=input;
errorfile[logfilesize1-4]='e';
errorfile[logfilesize1-3]='r';
errorfile[logfilesize1-2]='r';
logfile="C:/Temp/"+logfile;
fstream dataFile2( logfile, ios::in);
if (dataFile2)
{
dataFile2.close();
}
else
{
cout << "ERROR: Cannot open logfile.\n";
}
dataFile1.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}
I believe your getline doesn't bother looking the newline but only for a $. You didn't post the file you are reading from, but check to ensure it has a $ at the end of the file name otherwise it will fetch the entire file.
It appears that unless you put a \n or endl after writing to the file using ofstream, ifstream won't be able to read anything from the file. In fact, adding a space after whatever you've written into file won't help either.
So always add a newline right after whatever it is that you've written to file using ofstream.