简体   繁体   中英

GCC iostream fstream error in Ubuntu 13.10

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 ac 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.

问题是您要混合使用C和C ++代码并使用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++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM