Fgets and Fputs

I am trying to read in and combine three files and output it as another file. I know I can do this using system commands, but its just one phase in a larger program. I believe I am using fgets and fputs incorrectly... I am getting an error of
cannot convert `std::string' to `char*' for argument `1' to `char* fgets(char*, int, FILE*)'

and

cannot convert `std::string' to `const char*' for argument `1' to `int fputs(const char*, FILE*)'


I could use some help understanding what I am doing wrong and then maybe if you can a little direction for how to fix it. I want to learn to write my own code so I would prefer help not the actual code, or an example of it being done correctly with a different application or with different file... I don't want to just cut an copy code.... Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// basic file operations
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;




int main () {
    
        FILE * File1;
        FILE * File2;
        FILE * File3;
        FILE * outFile;
        string file1;
        string file2;
        string file3;
        
        outFile = fopen("outFile.run","a");
        File1 = fopen("JCP_WK13ABT3.sign","r");
        if (File1 == NULL) perror ("Error opening file");
   else {
     fgets (file1 , 10000 , File1);
     fputs (file1 ,outFile);
     fclose (File1);
   }
   
        File2 = fopen("samples", "r");
        if (File2 == NULL) perror ("Error opening file");
   else {
     fgets (file2 , 10000 , File2);
     fputs (file2 ,outFile);
     fclose (File2);
   }
       
        
        File3 = fopen("split", "r");
        if (File3 == NULL) perror ("Error opening file");
   else {
     fgets (file3 , 10000 , File3);
     fputs (file3 ,outFile);
     fclose (File3);
   }
            
         
        
         fclose(outFile);
        
        return  0;}

Why do you want to use FILEs? Why not just use ifstreams and an ofstream?
Last edited on
fgets and std::string don't play nice together.

As LB suggested, you're better off with fstream instead of FILE.

or if you want to stay with FILE, you'll need to have some char arrays instead of std::strings.

But I really recommend the fstream route as it's simpler and less error prone.
Thanks guys,

I have never used ifstreams or ofstreams before, so I am sure that will take a little time to read and get familiar with, but I definitely appreciate your suggestion. I will check into and try to learn it. I am sure this site has some info on them I can check out. :-)
I am trying to understand this, but I'm having all sorts of trouble... does anyone have sample code from a program that uses the ifstreams and ofstreams and does anything with the data from one to the other?
Topic archived. No new replies allowed.