My c++ codes doesn't create or edit text file - c++

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");
}

Related

Why doesn't ofstream create a new file?

I have a problem where ofstream isn't creating a text file. I compile and run the program just fine with no errors and I am 100 percent certain that I am in the right directory and that there are no spelling mistakes or errors. What is strange is when I move my program to my C drive or onedrive folder, then the text file is created, it just doesn't work in any of my other folders. I don't know what the problem is and I have scoured the internet for the past couple hours trying to find a solution, but I think it has something to do with my file permissions perhaps?
Here is my code, it's just a really simple program:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream myfile;
myfile.open("newnumbers.txt");
}

How to make a C++ program that works with a .txt file without showing it

My program needs to use a hidden text file to keep track of user's name.
But when the program starts, if it can't find the 'Name.txt' file in the same directory, it generates one that is visible to the user.
The user can view it, edit it, and so on. How can I prevent this from happening, so that only my program can modify the file?
Also, is there a better way to keep knowledge of the name of the user (keep in mind I'm new to programming in general, not only to C++)?
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <Windows.h>
using std::string;
using std::cout;
using std::cin;
using std::ifstream;
using std::ofstream;
int main()
{
string line;
ifstream example;
example.open("Name.txt");
getline(example, line);
if (line.compare("") == 0)
{
example.close();
string con;
cout << "Welcome to this program!\n";
cout << "Do you want to register? (y/n) ";
cin >> con;
con[0] = tolower(con[0]);
if (con.compare("n") != 0)
{
string name;
ofstream one;
one.open("Name.txt");
cout << "What's your name? ";
cin >> name;
one << name;
one.close();
cout << "See you later " << name << ".";
Sleep(4000);
}
}
else
{
cout << "Welcome back " << line << ".";
example.close();
Sleep(4000);
}
}
EDIT : I just realised I said 'to keep track of the user'. Now I realized why you guys thought I wanted to do something bad with this program. I corrected it now, what I meant was 'to keep track of the user’s name'.
I understand that you want to maintain a file that contains the names of all the registered users, or some other kind of current-user-independent data.
The problem
Your code tries to open the file in the current working directory of the program. Unfortunately, it depends on the way the user has launched your program.
It also ignores possible errors during the opening when reading the file. So if the file isn't there, your code will open the file as ofstream for writing (which will create the file if it doesn't exist).
How to solve it ?
To fulfill your requirements, you should open the file in a predetermined location (for example fixed during the installation process, or in the program's configuration). See this article, on where to ideally store data and configuration files on windows platform.
If you want to make sure that the program only opens the file if it already exists, you should verify the result of the open on the ifstream and issue an error message if this failed:
example.open("Name.txt");
if (!example) {
cout << "OUCH ! Fatal error: the registration file couldn't be opened !" <<endl;
exit (1);
}
How to protect the file against users ?
Note however that if your program reads and writes data from the file, the user could find it also and edit it manually. This will be difficult to prevent.
Alternatively you could consider using the windows registry, which is less trivial for the user to edit (although not impossible). The major inconvenience of this approach is that it's system dependent and it will make the porting of your code to other platforms much more difficult.
If you want to fully protect your file, you could as suggested by Chris in the comment, encrypt the file. Encryption is complex business; Consider using a library such as openssl or a proven algorithm.
This will protect you against ordinary users. But you'd still be exposed to hackers able to reverse engineer your code and to find the encryption key that must be somehow embedded in your code to decrypt the file.

cannot create a text file in C++

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.

unable to open file stream c++

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.

Can't get a simple ifstream to work in Visual Studio Express

I am trying to learn C++ and am on the file input/output section. I've hit a brick wall because my test application just plainly isn't working in Visual Studio Express 2012. Here is my code:
// ConsoleApp03.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file_reader;
file_reader.open("C:\temp.txt");
// Test to see if the file was opened
if (!file_reader.is_open() ) {
cout << "Could not open file!" << endl;
return 255;
}
string line;
// Read the entire file and display it to the user;
while (getline(file_reader,line)) {
cout << line << endl;
}
// Close the file
file_reader.close();
return 0;
}
Every time I run this, I get "Could not open file!". I have verified that the file being opened does exist, and I have sufficient permission to read. I have tried other text files, including in other different locations like my documents folder, but the result is always the same. My text file is very simple and only contains two lines of text. I am abel to open this file in Notepad++, and the file has no special attributes (system, Read only, etc). I have even tried converting the file to/from ANSI and UTF-8 with no luck.
I have looked at other problems similar to what I have here, but these don't seem to be applicable to me (e.g.: ifstream::open not working in Visual Studio debug mode and ifstream failing to open)
Just to show how simple the text file is, here is me typing it from the command prompt:
C:\>type C:\temp.txt
Hi
There
This may or may not fix your problem, but \ followed by char is an escape sequence. So your file path is actually invalid. Try
file_reader.open("C:\\temp.txt");
The \t actually means tab. See here.