I am working with Xcode and I am having trouble opening a file stream to assign variables from a text file. I speculate that placing the txt file in the same directory as the project would allow me open the stream without including the entire dir. I have been messing with this for a little while to no avail can I get it to work properly. I believe I was able to read data at one point, but I think the string printed was in unicode (not sure). It is a very simple program.. I would think that it would work.
I think my problem has to do with the directory the example is in and how Xcode works with project files. I just put the example file in the project folder and hoped that it would work.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name;
ifstream infile;
infile.open("example.txt");
if(infile.is_open())
{
infile >> name;
}
else
cout << "Unable to open file";
cout << name;
return 0;
}
First of all, remember, that working directory is not always the same directory where the program's binary resides.
Change:
infile.open("example.txt");
to:
infile.open("/full/path/to/program/directory/example.txt");
where /full/path/to/program/directory/ is the location of folder, where program (and thus example.txt file) is placed. It should fix the issue.
By the way, you may also want to read this question, that addresses very similar problem.
Also, read about getcwd() function.
Related
So i'm making an extremely simple guessing console game and i want to store data permanently in a file (highscore). However everytime i compile the file i'm using empties itself. Is there anyway to stop that?
I've tried a lot of thing which didn't work and i honestly don't know where the problem is. I'm guessing it has to do with the fin and fout but for others it seemed to work
#include <iostream>
#include <fstream>
#include <time.h>
#include <conio.h>
int hs;
//this would be the play_game() function, unrelated to the subject
int main()
{
std::ofstream fout;
fout.open("HS.txt");
std::ifstream fin;
fin.open("HS.txt");
srand(time(NULL));
//menu with 4 options, play, quit, help and highscore (which i'm working on)
fin.close();
fout.close();
}
Don't open your file twice in parallel with two streams. Also, a simple open-for-writing will not truncate your file, but will place you at the start of the file, so you'll be overwriting existing data; see this question. You have to open files with the write mode.
You need to either:
Open your file for both input and output - and without truncating it; see: What does it mean to open an output file as both input and output? , or
Open your file for reading only when your app starts, and open it for writing, and write to it, when it exists (or every once-in-a-while for better resiliency).
I searched this problem on web and I found some solutions but they didn't solve. So I must ask to someone. My problem is about c++, I am writing normal code but any file operations command doesn't work, they don't create or change text files. After It work, doesn't create a text file on same directory I tried to change my ide, I tried to run as Admin, I tried to open show secret folders, I tried to change compiler's bit, I tried to run on D:, I turned off my anti-virus app but they didn't solve. I added system("dir") the end of the codes and it showed to me the text file(i couldn't see in the folder again) but after this, I went to same directory with cmd(as admin) but this time i couldn't see the text. I couldn't remember what i've tried more. Also I have some problems about permissions i don't know maybe this about the my problem. Here is a part of my codes;
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
void adding() {
string userName;
cout << "Enter your nickname: ";
cin >> userName;
ofstream userFile;
userFile.open("users.txt", ios::app);
userFile<< userName;
userFile.close();
cout<<"The user is succesfully added!"<<endl;
}
int main()
{
cout<<system("cd");
adding();
system("dir");
}
I'm using an ofstream object to create and print a string to a text file, but it doesn't work. this is my code :
#include <iostream>
#include <fstream>
using namesace std;
int main()
{
ofstream output("d:\\data.txt");
output << "this is my text" << endl;
output.close();
return 0;
}
The file data.txt was created when I set output("data.txt"). The text file was created in the same folder that contains the source code. But when I set output(d:\\data.txt) or any other location, it was not created at all. This code has also worked well in other computer and the problem only occurs in my laptop. I'm using visual stdio 2013 and operated by
Windows 10 pro.
Try making a file manually in d:\\, then get the complete, correct directory of the file from its properties. That way, you will know any mistakes you are making in specifying the directory of the file to be created.
I am a beginner c++ programmer and I was accustomed to using Visual Studio before, but i am now using mac and i use xcode now. In xcode, i am not able to read data from the file (so i am also not able to get the output.) Which setting should i change in xcode to read data from the file(the file is in the same folder as the project) properly ?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//in thefile.txt i only have a string "hello"
int main() {
string text; //since i only have a string in the file. So this is a variable representation for it
ifstream myfile;
myfile.open("thefile.txt");
myfile >> text; //extract the word from the file
cout << "The following is the data in the file: " << text <<endl; //trying to print The following is the data in the file: hello
return 0;
}
//Output is "The following is the data in the file: "
Probably the most straightforward way is to use a fully qualified file name to take the guesswork out of it. If you're not sure of the full name, you can open up a window in the OS X Terminal program, type "file ", and then drag your file from a Finder window onto that command line. Terminal will fill in the full path. "file" is an innocuous command that will tell you information about your file type. But the important thing is, you will have a full path that you can then copy from Terminal and paste into your code.
If your file path has blanks in it, you may have to manually remove the backslash characters that will appear.
Xcode doesn't have a setting per se (that I know of) to determine what directory is active when you launch your program. But a full path takes the guesswork out of it.
I'm fairly new to C++. I'm creating a code that will input a file and output the results in an output file, and using stacks and junk.
But what i want to do is create a file based on a user input. Asking the user (when a file doesn't exist in a specific directory) if they would like to create that empty file. I've done this on C# using Directory and Dictionary, but C++ isn't really clicking for me. Here's the snippet of my code (I'm not going to paste 200+ lines for one thing) and where i want to do. Ignore the comments. It's just to keep track of what I'm doing.
if (file.is_open()) //if the file is open (and works)
{
string output;
cout << "Please enter the full directory of the file you would like to have the results in" << endl;
cin >> output;
output.c_str();
file_result.open(output); //open the results file for checking answers
while (file_result.fail())
{
cout << "This file does not exist. Would you like to make one?" << endl;
}
As you see, where I ask the user if they would like to make that file is where i would want this to be.
Any help would be lovely! Transitioning from C# to C++ was a bad idea.
You can open a file for writting (in append mode) in this way:
std::ofstream ofs;
ofs.open (output.c_str(), std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
More information about file operations can be found here:
http://www.cplusplus.com/reference/fstream/ofstream/open/
The most basic way to create a file based off of user input is this, You should how ever include checks to make sure the path is valid and that no file exists etc.. I only have time to show you how to do this.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ofstream outFile;
string path;
cout << "Please enter the full path for your file: ";
getline(cin, path);
outFile.open(path);
return 0;
}
What's happening here is quite simple the user inputs the full path (C:\Hello.txt) it's read by getline(cin, path) and is stored in path.
outfile then creates that file.
Please make sure you add checks to validate no file with that name already exists etc.. I'll update this later with a better example but this will create a file for you