C++ ifstream is_open() fails [duplicate] - c++

This question already has answers here:
Where to place file in order to read?
(4 answers)
Closed 6 years ago.
Why does my is_open() always fail and goes into the else statement which displays the error message?
Another method of mine is similar to this yet it worked.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string userName;
cout << "Please login to your account here!" << endl;
cout << "Username: ";
cin >> userName;
ifstream openSalt;
openSalt.open("salt.txt"); //open the file
if(openSalt.is_open()) //if file is open
{
string temp;
while (getline(openSalt,temp))
{
//gets content
//if user exists, login
//else, exit
}
openSalt.close();
}
else
{
cout << "Error opening file!" << endl;
exit(1);
}
return 0;
}

Assuming code is main function etc, the code works for me.
The issue is more likely to be that what ether tool/IDE you are using to compile the program sets the current folder to a different place to what you are expecting, and then is not the place the salt.txt file is in.

Check two things.
If the file really exists in the current folder from where you program is run ?
Do you have correct permissions to open the file ?

use this function to get the error :
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
then call it in your else
else
{
cout << GetLastErrorAsString() << endl;
system("pause");
exit(1);
}
I used your code and it worked, make sure the file is in the same directory, make sure you are not working with a salt.txt.txt file (common mistake)

Related

Moving files from one directory into another

I would like to copy all files from test1 into test2. The code compiles but nothing happens.
#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
string input1 = "C:\\test1\\";
string input2 = "C:\\test2\\";
MoveFile(input1.c_str(), input2.c_str());
}
I was considering xcopy but it would not accept a pre defined string. Is there a work around?
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
int main()
{
string input1 = "C:\\test1\\";
string input2 = "C:\\test2\\";
if (!MoveFile(input1.c_str(), input2.c_str()))
{
string msg = GetLastErrorAsString();
cout << "fail: " << msg << endl;
}
else {
cout << "ok" << endl;
}
system("pause");
}
Your code works for me, you may have to set the character set to use multi-byte character set in your project properties.
If not, provide us with the error.
Check if you have the write rights on C:.
Check if there already is a test2 folder in C: (or if there is not a test1 folder in C:).
I resovled the issue by removing the \\ from test2. Folder test 2 doesn't exist. Thank you for the replies and the test code. I think SHFileOperation will be a better option as I have to transfer files from a floppy to my C drive. string input1 = "C:\\test1\\";
string input2 = "C:\\test2";

Simple ifstream not opening any file

Newb here. I have spent the last 4 hours trying to solve this problem.
ifstream is suddenly not opening files.
ofstream has no problems writing to the file.
The file exists, it's contents are, ThisIsText, and it is in the reference directory, which I confirmed with system("dir & pause")
I tried Code::Blocks and Dev C++, but I think they're using the same compiler(GNU GCC Compiler).
I tried using the full filename path with double backslashes.
I see people mentioning permissions, but I don't know how to tinker with that.
I'm on Windows 10.
Edit: I just found a new compiler(Embarcadero 10.1 AKA Borland) and the code works with it. I still want to know what the problem is with GNU GCC
The following code skips to the else statement:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string message;
ifstream inputFile;
inputFile.open("File.txt");
if (inputFile.good())
{
inputFile >> message;
cout << message;
system("dir & pause");
}
else
{
cout << "failed to open input file\n";
system("dir & pause");
} return 0;
}
If it helps, I found the following code online and it outputs, "Error code = 2"
from winerror.h, that is: ERROR_FILE_NOT_FOUND
#include <windows.h>
#include <iostream>
int main()
{
HANDLE hFile = CreateFile(
"one.txt", // Windows does not case about case
GENERIC_READ,
0, // no sharing
NULL, // default security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL ); // no file template
if(INVALID_HANDLE_VALUE == hFile)
{
DWORD errCode = GetLastError(); // see winerror.h for meanings
std::cout << "File wouldn't open :-(" << std::endl;
std::cout << "Error code = " << errCode << std::endl;
}
else
{
std::cout << "File opened OK :-)" << std::endl;
CloseHandle(hFile);
}
return 0;
}
PS. I know using namespace std; is bad practice

C++ File input and output:cannot input

The following C++ code is to read a txt file and then write the numbers of chars in each line and the number of all chars in the txt file.I use MS Visual Studio 2008.But something is wrong.Only the number of all chars is input into the txt files,but the numbers of each line are not input into the txt files.Now I cannot figure it out.Could someone give me some advice?Thanks a lot!
And my another question is what should I do to insert something in the middle of the txt file?
This is my Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
++cnt;
if ('\n' == ch)
{
ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
inOut << cnt;
inOut.put(' ');
inOut.seekg(mark);
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
The txt file before running:
The txt file after running:
The result in command line:
I don't know fstream's very well, but I think you want to change the way you are doing this. You can think of a file as a contiguous piece of memory. Appending on the end is easy, but inserting in the middle can be problematic. In particular, if you do insert something, then your seekg might not be valid.
I would recommend three strategies:
Understand what is going on currently (try closing file before seekg and see if anything gets written by the inOut << cnt;)
Read from one file, write to a different file -- this will most likely be more efficient and less complicated than trying to modify the file in place.
Read from source file, store and modify in memory, write out modified buffer to original file. For large files, this might be less efficient than #2, but it means you don't need two files on disk.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
int cntline=0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
//++cnt it include '\n' it should put behind of "if"
if ('\n' == ch)
{ ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
cntline=cnt-cntline; //add it to account the number of chars of each line
inOut.seekg(0,ios::end); //here!!!!!!!!!!!!!!!!!!!!!!!!!!! add this , the resault is finally right! the reason it's down here.
inOut << cntline;
cntline=cnt; // and here! it can assure the number of the chars of each line be right!
inOut.put(' ');
inOut.seekg(mark);
}
else
{
++cnt; // it's here!
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
inOut.seekg(0,ios::end)
i'am not sure it,but you can take it a reference.
you open file with "ios::app",so the VS 2008 only allows you to add text start from the end of file (it won't happened in vc 6.0).
if the file is :
it's compile by
vs 2008
when get the first '\n' the file pointer is pointing to'vs 2008'(yes,it just like the common pointer point the string ). and i debug it then find a value of the stream object named _Wrotesome .its value is false!
so i think the compiler think the file pointer point at o const sting.so you just can't write anything whis this position. so i add the code inOut.seekg(0,ios::end);.now you
can write anything you want to this text!
wish this can help you !
I don't know why do you choose std::fstream as your tool. If std::fstream is not asked or necessary, I would like to provide a example to solve your problem. Here's some limitation:
a. I read all the file into memory in one ReadFile operation. If your file is large, you may replace it with a loop.
b. I suppose your line separator is '\n'.
c. I add [line_count] before the '\n', which look like better.
here's my code.
#include "windows.h"
#include "sstream"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile = ::CreateFile(L"C:\\Users\\wujian\\Desktop\\pingback - Copy.log", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE )
{
DWORD dwSize = ::GetFileSize(hFile, NULL);
if (dwSize)
{
char *pBuffer = new char[dwSize];
DWORD dwRead = 0;
::ReadFile(hFile, pBuffer, dwSize, &dwRead, NULL);
if (dwRead == dwSize)
{
std::stringstream ss;
int iPos = 0;
int iLine = 0;
while (iPos < dwSize)
{
if (pBuffer[iPos] == '\n')
{
ss << '[' << iLine << ']';
iLine = 0;
}
ss << pBuffer[iPos];
iPos ++, iLine ++;
}
ss << '[' << dwSize << ']';
::SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
DWORD dwWrite = 0;
::WriteFile(hFile, ss.str().c_str(), ss.str().length(), &dwWrite, NULL;
}
::CloseHandle(hFile);
}
}
return 0;
}

How to log all input (cin) into file in C++

Actually, I'm working on a minishell. My functions work, but when I want to log the whole cin stuff (commands, parameters, output) into a file, nothing appears in the file. Nowhere can I find something to handle with full input and output with parameters.
I hope someone can help me.
My code:
using namespace std;
ofstream outputFile;
void read_command(char *com, char **par){
fprintf(stdout, "$");
cin >> com;
outputFile.open("logging.txt"); // file opened but nothing APPEARS IN IT????
if(strcmp(com,"date")== 0){ // DATE
time_t rawtime;
time ( &rawtime );
printf ( "%s", ctime (&rawtime) );
}
else if(strcmp(com,"echo")== 0) // ECHO
{
string echo_part;
cin >> echo_part;
cout << echo_part << endl;
}
else if(strcmp(com,"sleep")== 0){ // SLEEP
int howlong = 0;
cin >> howlong;
cout << "seconds: " << howlong << "....zZZzzZzz" << endl;
sleep(howlong);
}
else if(strcmp(com,"ps")== 0) // PROCESS
{
execlp("/bin/ps","ps","-A",NULL); // ps - command
}
}
void handler(int p) { // CTR-C handler
cout << endl;
cout << "Bye !" << endl;
outputFile.close();
alarm(1); // 2 seconds alarm ends process with kill
}
int main(){
int childPid;
int status;
char command[20];
char *parameters[60];
signal(SIGINT,&handler); // CTR-C exit disabled
while (1) {
read_command(command, parameters);
if ((childPid = fork()) == -1) {
fprintf(stderr,"can't fork\n");
exit(1);
}
else if (childPid == 0) { //child
execv(command, parameters);
exit(0);
}
else { // parent process
wait(&status);
}
}
}
You re-open the output stream outputFile for every line, overwriting the file with each new command.
Edit: As the other posters noted, not actually writing something to outputFile might be a second reason.
You open outputFile, but never write anything to it. What should appear there?
To output something to the file, try
outputFile << something
there are no
outputFile << ...;
so you are not writing to the file
Your code contains a lot of potential memory access violations.
There are libraries to help you in what you are trying to do (reading and interpreting user input), for instance the GNU Readline library, which is coded in C (but can be used by C++ code, as is the case for all the C-written libraries).
There are some nice C++ wrappers, such as for instance SReadlineWrapper.

c++ sprintf function and fstream to create/check a text file

I am having some trouble with sprintf and fstream functions in order to create new text files for a POS program/check whether the file already exists. I don't know if i am doing something wrong because the same set of functions works fine in other places in my code...
This particular section of code is taking input from the user to create a details file, the name is made up of the first and last name details that were entered into the system. For some reason the new file is not being created. When I step through the program I can see that the custDetC variable is being filled with the correct data. I have also included the file existence check as it may or may not have something to do with the issue at hand...
Tony Mickel
sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
cout << custDetC << endl;
FileEX = FileExists(custDetC);
if (FileEX == true)
{
fopen_s(&custDetF,custDetC, "rt");
fprintf(custDetF, "%s %s\n", firstName, lastName);
fprintf(custDetF, "$d\n", phoneNo);
fprintf(custDetF, "%s $s\n", unitHouseNum, street);
fprintf(custDetF, "%s %s %d", suburb, state, postCode);
fclose(custDetF);
}
else
{
char *buf = new char[100];
GetCurrentPath(buf);
cout << "file " << custDetC << " does not exist in " << buf << endl;
}
}
bool FileExists(char* strFilename)
{
bool flag = false;
std::fstream fin;
// _MAX_PATH is the maximum length allowed for a path
char CurrentPath[_MAX_PATH];
// use the function to get the path
GetCurrentPath(CurrentPath);
fin.open(strFilename, ios::in);
if( fin.is_open() )
{
//cout << "file exists in " << CurrentPath << endl;
flag = true;
}
else
{
//cout << "file does not exist in " << CurrentPath << endl;
flag = false;
}
fin.close();
return flag;
}
You seem to be opening the file for reading, but you need to open it for writing.
Instead of "rt" use "wt" in fopen_s()