I have been working on C++ for few years now and have compiled stuff several times but the following issue is totally new to me and it just doesn't make sense.
Following are the steps i am following:
using cygwin setup with g++ version:6.4.0 and clang++ version:4.0.1
created a new cpp fie using sublime text added simple cout and compiled
with the command: clang++ -g -Wall -std=c++14 thread1.cpp -o thread, works fine.
added new contents maybe another cout, this time upon compilation i
get a ton of errors stating its not utf-8 file.
saved the file using utf-8 encoding in sublime text and also tried
with utf-8 BOM encoding, still getting same not utf-8 file error.
ran the file command in cygwin to check file encoding , file -i
thread1.cpp, got output as thread1.cpp: text/x-c; charset=utf-8.
Any pointers to what might be going wrong here?
Following is the code which compiles:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello";
return 0;
}
Following code gives error:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello World";
return 0;
}
Following is a snippet of the errors generated:
./thread:3:29: error: source file is not valid UTF-8
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:3:30: warning: null character ignored [-Wnull-character]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:3:31: warning: missing terminating ' character [-Winvalid-pp-token]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:4:3: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
^
./thread:4:4: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
^
./thread:4:5: error: source file is not valid UTF-8
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>.
By doing #include "thread", you've said (by convention at least) to check the current directory for a thread file first. The problem is that you have a thread file in your current directory. Presumably, it's your executable from the first time you compiled. This is apparent by the errors, which explicitly say ./thread.
You should include all standard headers via #include <thread> rather than #include "thread" - you never want to search elsewhere first for standard headers.
Related
I am setting up my environment to run a C++ program on VSCode in Windows 10. I have MINGW installed and the path (C:\MinGW\bin) added to my environment variables. I have added the appropriate extensions in VSCode.
In my hello world program. It works just fine. However, when I open a file, nothing prints from anywhere in the entire program.
I am compiling like this:
g++ main.cpp -o main
And running the program like this:
./main test.txt
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
cout <<"Here\n";
ifstream file; //This is the line that causes no output
file.open(argv[1]);
return 0;
}
The file is not the problem. I have tried multiple file types like .txt and .csv.
Based on the response to Size of file using C++17, I wrote the following program. But, when the executable is run, I get a segmentation fault. I am using g++ 8.3.0 on an iMac running High Sierra.
// c17filesize.cpp
// Jul-02-2019
#include <cstring>
#include <filesystem>
using namespace std;
int main(int argc, char **argv)
{
char filename[100];
(argc > 1) ? strcpy(filename, argv[1]) : strcpy(filename, __FILE__);
auto size = filesystem::file_size(filename);
}
Prefer using string (and its contructor) over c style strings.
according https://en.cppreference.com/w/cpp/filesystem/file_size you must specify full path. Did you verify argv[1] holds a full path?
check the file exist before attempting to read its size std::filesystem::exists(filename);
use try and catch sections to catch an exception.
Just bumped into segfault whilst using std::filesystem:exists.
With GCC 8.3. Had to link with -lstdc++fs to solve the problem.
Note: GCC 9+ solves this problem (https://gcc.gnu.org/gcc-9/changes.html)
Compiling with gcc 9.1.0 went through successfully without any qualms.
I am using Ubuntu 13.10. I am getting some errors for the following code.
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
if ( argc != 2 )
{
// printf(argv[0] + " usage: fifo_client [string] \n");
/// cout << argv[0] << " usage: fifo_client [string]" << endl;
exit(EXIT_FAILURE);
}
ofstream out(fifo_file);
if(out)
out << argv[1] << endl;
return(EXIT_SUCCESS);
}
If I run the above program a.c using command
gcc a.c -o a
a.c:1:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
I don't know whats the problem.
Use g++ instead of gcc. gcc could compile a c++ file if it had the right extension (.cpp for instance) or with the right arguments (-x c++) but adding the arguments needed to link with the C++ libraries is far too complex to avoid the simple solution.
The problem is that you're mixing C & C++ code and compiling it using GCC.
try
#include <fstream>
using namespace std;
instead of #include <fstream.h>
anyway your source code is not full to make correct suggestion.
I ran your code in my compiler and got following error :-
test2.c:3:21: fatal error: fstream.h: No such file or directory
#include <fstream.h>
^
compilation terminated.
so i think your question has typo.
It is because you are mixing c and c++ code, fstream is part of c++. try to run by g++.
This question already has answers here:
main.cc:5:30: fatal error: folder/file.h: No such file or directory
(3 answers)
Closed 9 months ago.
I began learning MinGW to compile C++ program. I had a sample C++ file including test.cpp (main program) and srfft.h (extra header file I added, not from the libray). The process executed as below step:
g++ test.cpp -o test.exe
test.exe
#include <iostream>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
I got the correct answer, but when I added #include in the C++ code as below:
#include <iostream>
#include <srfft.h>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
CMD showed me" fatal error: srfft.h:No such file or directory"
How do I execute my code with MinGW?
Where is the problem?
Try
#include "srfft.h"
Notice "" instead of < and >. With "srfft.h", the file is searched relative to the current directory, whereas with <srfft.h> the search only takes place in the system and specified include directories, which normally doesn't include the current directory.
I am just learning the very basic aspects of input/output streams, and can't seem to have my program read a text file. It gives me errors that indicate it is trying to read the .txt file as C++ code, while I am just using values in there to test my stream.
These are the contents of my included .txt file:
12345
Success
And here is the main program's code:
#include <fstream>
#include <iostream>
#include "C:\Users\Pavel\Desktop\strings.txt"
using namespace std;
int main (int nNumberOfArgs, char* pszArgs[])
{
ifstream in;
in.open("C:\Users\Pavel\Desktop\strings.txt");
int x;
string sz;
in << x << sz;
in.close();
return 0;
}
The first error message I receive is "expected unqualified-id before numeric constant" which tells me the program is attempting to compile the included file. How can I prevent this and have the text file read as intended?
Don't #include your .txt file. Includes are for source code. They textually insert the file into your code, as if you had actually copy-pasted it there. You shouldn't be #includeing a file you're opening with an ifstream.
Opening files on the filesystem at runtime doesn't require any mention of that file's name in the source code. (You could, for instance, ask the user for a filename, and then open it just fine!)
The case where you might #include data in your source would be if you wanted to have that data embedded into the executable of your program (and thus not rely on a file that was on the filesystem when running). But to do that, you have to format your file as a valid C++ data declaration. So it would not be a .txt file at that point.
For instance, in strings.cpp
#include <string>
// See http://stackoverflow.com/questions/1135841/c-multiline-string-literal
std::string myData =
"12345\n"
"Success";
Then in your main program:
#include <iostream>
#include <sstream>
#include "strings.cpp"
using namespace std;
int main (int nNumberOfArgs, char* pszArgs[])
{
istringstream in (myData);
int x;
// Note: "sz" is shorthand for "string terminated by zero"
// C++ std::strings are *not* null terminated, and can actually
// legally have embedded nulls. Unfortunately, C++ does
// have to deal with both kinds of strings (such as with the
// zero-terminated array of char*s passed as pszArgs...)
string str;
// Note: >> is the "extractor"
in >> x >> str;
// Note: << is the "inserter"
cout << x << "\n" << str << "\n";
return 0;
}
Generally speaking, just #include-ing a source file like this is not the way you want to do things. You'll quickly run into trouble if you do that in more than one file in your project (duplicate declarations of myData). So the usual trick is to separate things into header files and implementation files...including the headers as many times as you want, but only putting one copy of the implementation into your build process.
An #include directive works the same way regardless of the extension of the file being included - txt, h, no extension at all - it doesn't matter. How it works is the contents of the file are pasted into your source file by the preprocessor before that file is passed to the compiler. As far as the compiler is concerned, you might as well have just copied and pasted the contents yourself.