ifstream of <fstream> won't compile - c++

I #include these headers:
#include <iostream>
#include <fstream>
but however this piece of code:
ifstream inFile;
still wont compile. what could be the problem? Im using Visual Studio 2010, Win32 C++.

You can put a using namespace std; at the top of your code so you don't have to fully qualify standard C++ stuff, but it's considered bad form by a large number of developers.
I simply prefix the standard stuff with std::, which makes the code longer:
std::cout << "Hello, world.\n";
but keeps me out of trouble vis-a-vis namespace clashes.
The following transcript shows the use of std:: prefixes in action:
$ cat testprog.cpp
#include <iostream>
#include <fstream>
int main (void) {
int n;
std::ifstream inFile("input.txt");
inFile >> n;
std::cout << "File contained " << n << '\n';
return 0;
}
$ cat input.txt
42
$ g++ -Wall -Wextra -o testprog testprog.cpp ; ./testprog
File contained 42

The type is std::ifstream. You must write it out in full, unless you brought the qualified name into scope by another means.

Related

Trying creating and writing into a txt file in C++

Basically, I'm following a simple tutorial about files handling in C++.
I've been trying to create and write into a txt file at the same time, but any of the methods I've tried won't actually create a txt file in my executable location.
I should also say that, I print myfile.is_open() just to know if the file truly created and opened, but I get 0 everytime with every method.
What am I doing wrong ?
I mainly tried to create and write to a txt file like this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open("example.txt", ios::out);
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
First, I bet you're using an IDE like Visual Studio. Most IDEs set your working directory somewhere other than your project directory. I don't use Visual Studio, but many of them put them in ../.
So your file is being produced, but not where you think you should find it.
If you compile and run this program without an IDE, you'll get your file where you expect it.
You may also be able to tell your IDE that the working directory should be your project directory.
Now, to keep you from making a few bad habits, I'm going to tell you two more things.
It's considered a mistake to do using namespace std. Instead, I do using statements only on those things I am going to use frequently. In your short code, I wouldn't have done any.
Next, if you're going to write out a file, it's better to use std::ofstream. It's otherwise the same code. But it's a bit clearer that you're only using the file for output.
So my version of your code:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream myfile;
myfile.open("example.txt");
std::cout << myfile.is_open() << std::endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
Yeah, those std:: everywhere can be annoying, so you could do this:
#include <iostream>
#include <fstream>
using std::ofstream;
using std::cout;
using std::endl;
int main()
{
ofstream myfile;
myfile.open("example.txt");
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
I actually have an include of CommonUsing.h that I put a few things I do almost everywhere.
#pragma once
#include <chrono>
#include <iostream>
#include <date/date.h>
//======================================================================
// The most common using statements I do in most of my code.
//======================================================================
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using namespace std::chrono_literals;
using date::operator<<;

Declaring multiple ifstream makes code crash

I just installed mingw on my Windows 10 computer and wanted to code a program that read two files. I immediately faced a frustrating bug with ifstream: when I declare more than one ifstream, the program seems to crash (nothing is logged although the first line cout some text).
The following code compiles and logs "test" in the console:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "test" << endl;
ifstream test;
return 0;
}
The following code compiles but seems to crash at runtime, nothing is logged:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "test" << endl;
ifstream test;
ifstream test2;
return 0;
}
I tested the exact same codes on a macOS Mojave and both codes work and log "test".
I guess the issue is related to the g++ installation but I'd like to know what's really happening and how I can fix this on Windows.

C++ _byteswap_ulong was not declared in this scope

when I try to compile my program I get this error:
error: ‘_byteswap_ushort’ was not declared in this scope
long lNum = (long)_byteswap_ushort(iNum);
this is the program:
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
long inputNum;
cout << "Input number:\n";
cin >> inputNum;
long Num = (long)_byteswap_ulong(iNum);
stringstream oss;
oss << hex << Num;
string mystring = oss.str();
return 0;
}
I thought that including stdlib should solve the problem. Is there any other library I should include?
The program is compiled with:
g++ -m32 -o output32 prog.cpp
You seem to be trying to use a function specific to MS Visual C++ compiler, which is not available in GCC. Use an appropriate GCC builtin instead.
Seems like uint32_t __builtin_bswap32(uint32_t x) would be appropriate in this case.
Another option, if you wanted a portable solution, would be to use something like Boost Endian library.

make file is throwing below two error

Hello i have created four file fn.cpp header.h,main.cpp and makefile
I am getting two error plz help to fix it.
fn.cpp:1 error: string was not declared in this scope? why?
fn.cpp:2 error :expected ',' or ';' before '{' token?
header.h:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int fn(string);
main.cpp:
#include "header.h"
string s= " hello world";
int main()
{
fn(s):
}
fn.cpp:
int fn(string ss)
{
printf("%s",ss);
}
makefile:
all:hello
hello:main.o fn.o
tab g++ main.o fn.o-o hello
main.o:main.cpp
tab g++ -c main.cpp
fn.o:fn.cpp
tab g++ -c fn.cpp
The std::string class is defined in the <string> header. Include that instead of the C library's <string.h> header.
Also, you need to include "header.h" from both source files.
Finally, you can't pass a string object directly to printf; that's a C function which knows nothing of C++ classes. Either use C++ I/O:
std::cout << ss;
or faff around with C-style strings:
printf("%s", ss.c_str());
many small "out of c++ style" problems :)
use header #include <string> and avoid printf if you can, better use cout
c++ lovers would like this a little bit more:
fn.h
#include<string>
void fn(const std::string&);
fn.cpp
#include <stdio.h>
#include "fn.h"
void fn(const std::string& ss)
{
printf(ss.c_str());
}
hello.cpp
#include "fn.h"
std::string s = " hello world";
int main()
{
fn(s);
}
Makefile
all: hello
hello: hello.cpp fn.o

std::stoi missing in g++ 4.7.2?

I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured?
#include <iostream>
#include <string>
using namespace std;
int main(){
string theAnswer = "42";
int ans = std::stoi(theAnswer, 0, 10);
cout << "The answer to everything is " << ans << endl;
}
Will not compile. But there's nothing wrong with it.
std::stoi() is new in C++11 so you have to make sure you compile it with:
g++ -std=c++11 example.cpp
or
g++ -std=c++0x example.cpp
For older version of C++ compiler does not support stoi. for the older version you can use the following code snippet to convert a string to integer.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
string input;
cin >> input;
int s = std::atoi(input.c_str());
cout<<s<<endl;
return 0;
}