ios not recognized - c++

I am working on a Windows VC++2008 program that does fileIO, and have hit an issue that is really weird. in my #include directives I have
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
and then I have a method that actually does the fileIO, but when I try to open the file like this:
std::ofstream Output;
Output.open("Output/log.txt", ios::out);
my intelisense allows it, and even has correct auto completes, but my compiler throws an error of:
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2653: 'ios' : is not a class or namespace name
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2065: 'out' : undeclared identifier
when I read about the ofstream.open() it stated that whether the file to be opened is for input, output, or both should be specified, but ios should be automatically included by any other iostream #include directive, and this problem is not corrected when I insert the:
#include <ios> // directive
the compiler has no complaints when I remove the second argument, but I know that I should try, and specify just in case I want to go in and read from a file as well as write to it. did I do something wrong?

It looks like you forgot to prefix it with std:: and you haven't used using namespace std; (judging from the fact that you explicitly state the namespace for std::ofstream).
Try changing it to std::ios::out.
You should not need to #include <ios> manually.

As already noted, you need std::ios::out -- only you really don't need it at all. When you open an ofstream, it opens for output by default (likewise, an ifstream gets opened for input by default). I'd also advise initializing the object at creation rather than creating a sort-of uninitialized stream, then opening it separately. Taking that into account, you get rather simpler bit of code:
std::ofstream Output("output/log.txt");

Related

Cannot create a file/write to it, but can read an existing one in c++

I was just learning C++ file handling, but ran into an error immediately using this simple code to create a new file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("test.txt");
file << "test";
file.close();
}
File doesn't show up. When I use .is_open(), it always gives 0. Program does compile though.
Then I manually created a .txt file with some text and tried to read it, and it worked. I supposed it was permission thingy, but it seems like all files are available to be changed? I'm not sure I completely understand how to check the permission though (I'm a bit new to all of this...), so please do help with it as well!
I use Atom and its terminal, my compiler is MingW, but I guess it might be a bit too old.
I tried to include the whole path, but it didn't work.
Thank you!
EDIT:
Just tried this code:
ofstream file;
file.open("C:\\Users\\username\\Desktop\\my_folder\\test.txt");
file << "test";
file.close();
Doesn't help. .is_open() gives 0 before I even try to write to a file.
EDIT:
Just tried this code:
fstream fileW;
fileW.open("write.txt", ios_base::in | ios_base::out | ios_base::trunc);
cout<<fileW.fail()<<endl;
cout<<fileW.is_open()<<endl;
fileW<<"Edit";
fileW.close();
Still doesn't work, returns 1 for .fail().
EDIT:
The error is "Permission denied".
EDIT:
Solved! I deleted Avast, it was blocking my program from accessing files.
Avast, I hate you. You are the worst.
Use
ofstream file("test.txt");
Your version does not create the file if it does not exist.
Similarly for input you should really use ifstream. fstream is best reserved for files you want to read and write from.

How do I create a directory inside the directory where my .cpp file is placed?

I am creating a directory in the desktop folder using this code.
#include <iostream>
#include <stdio.h>
#include <direct.h>
#include<string>
using namespace std;
int main()
{
string k,s="c:/Users/Dell/Desktop/";
cin>>k;
string p =s+k;
if (mkdir(p.c_str()) != 0)
{
cerr<<"Failed"<<endl;
}
return 0;
}
But this code will be specific to my pc only as it has the whole parent directory path.
I want to create a new folder inside my working directory where the main.cpp is placed, because we don't know where the user will place the cpp file.
I know there are a lot of solutions similar to this problem on the forum but I couldn't find or understand any that could help me here. Thanks.
Do you really mean the .cpp file?
You can get the filename as a string using the __FILE__ macro and then use std::filesystem::path to manipulate it, but are you sure this is really what you want to do? Are you expecting end users to compile this program in the location they want to run it?
You can also (in theory) get the path through which the program was executed using the argv argument to main, which seems more likely to be what you mean, but it's unreliable in a number of surprising ways ...

Error in ofStream, Qt 5.14.2 can 't write string into file

I am trying to write my program from VS (MVCS) for Qt(MinGb)
I am using ofstream and has a following code:
#include <fstream>
#include <string>
#include <iostream>
#include <exception>
using namespace std;
void WriteToFile(ofstream* fileToWrite, std::string StringNeedsToWrite)
{
if (fileToWrite)
{
if (IsStartOfNewString(StringNeedsToWrite))
{
*fileToWrite << '\n';
}
*fileToWrite << StringNeedsToWrite;
}
else
{
throw exception();
}
}
I want to write string into file, and set ofstream into my method.
But i have a strange error and don't know how to fix it (on image)
They differ in their make files and project files. A common problem is that moving a project from one environment to another entails big mistakes.
For example, MinGW under Linux does not have standard streams, but there is windows.h.
Such problems should be taken into account and studied at the stage of developing a program architecture and choosing technologies for development.
Frequent practice has shown that Qt is best used with your IDE and, if possible, use its classes for development.

First Instance of "STD_OUTPUT_HANDLE" Identifier is Undefined

I am using Visual Studio 2019, and my code uses console outputs which change colors frequently. I am including Windows.h in my code, which is the header file that contains SetConsoleTextAttributes, whereas STD_OUTPUT_HANDLE should be initialized by using namespace std. My code in its entirety can be found here, but the following is the section with the error:
#include <iostream>
#include <cmath>
#include "HeadFile.h"
#include <windows.h>
#include <string.h>
using namespace std;
int Play(char(&spaces)[7][6], int(&color)[7][6], int player, int playerOneWins, int playerTwoWins, int ties)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
.....
The code runs fine, but inside of studio itself, I see the following error:
The error is coming from the first instance of STD_OUTPUT_HANDLE only (another case at the bottom of the picture has no errors). If I comment out the first one, the next instance errors:
How can I fix this issue? I've read in a few non-related posts that using namespace std can sometimes lead to problems. Is this the case?
Use Header file "Windows.h" instead of "windows.h".
I had the same problem, adding #include <stdlib.h> fixed it for me.
I had the same problem, you need to use #include <Stdio.h>

no matching function for call to getline()

so i am working on something and i cant seem to get why it isn't working.
void display_alls()
{
ifstream fpd;
fpd.open("student2.txt",ios::in);
if(!fpd)
{
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch();
return;
}
while(fpd)
{
int pos;
string seg;
cout<<"\tUSN"<<setw(10)<<"\tName"<<setw(20)<<"Book Issued\n";
//fp.seekg(pos,ios::beg);
getline(fpd,st.usn,'|');
getline(fpd,st.name,'|');
getline(fpd,st.email,'|');
getline(fpd,st.phone,'|');
getline(fpd,st.stbno,'|');
getline(fpd,seg);
cout<<"\t"<<st.usn<<setw(20)<<st.name<<setw(10)<<st.stbno<<endl;
}
fp.close();
}
[Error] D:\library\library_v1.cpp:514: error: no matching function for
call to `getline(std::ifstream&, char[20], char)'
error is on each line with getline! but not in "getline(fpd,seg);"
this thing isn't working on MingW compiler but was working on my college system, idk maybe they are having older compiler, can you please tell me what is wrong.
much appreciated.
The error message suggests that the code is attempting to read into an array of 20 characters. Unfortunately std::getline does not deal in character arrays. It only reads into std::string, which is why it works with string seg; For character arrays you need to use std::istream::getline. Link to documentation page
However life will probably be easier for you if you can replace the character arrays in the data structure with std::strings.
std::getline, if that's what you're trying to use, is defined in the <string> header. You will want to include it:
#include <string>
Then you can call it via std::getline. If you get tired of typing std::, you can do:
using namespace std;
Depending on the IDE or build setup, these things can sometimes be done for you already. It's possible that is why this works on the school computer, but not yours.