简体   繁体   中英

C++ class argument different in the class, than the argument passed to the class

I am quite new to C/C++ programming, and I am trying to improve my understanding of file i/o. With this program I originally tried to make myFile a typedef in C (which obviously did not work), so I moved onto classes in C++ (which is why none of the code utilises iostream).

#include <stdio.h>
#include <stdlib.h>


// required myFile.file=fopen("path","mode");
// read from file: myFile.read(char* out);
// write to file: myFile.write(char* in);
class myFile {
      public:
    FILE *file;

    void open(const char *path, const char *mode) {
        file=fopen(path,mode);
    }

    /*void read(char *out) {
        fread(out, sizeof(out[0]), sizeof(out)/sizeof(out[0]*sizeof(char)), file);
    }*/

    void write(char *in) {
        fwrite(in, sizeof(in[0]), sizeof(in)/sizeof(in[0]), file);
        printf("%i : %s\n",sizeof(in),in);
    }

};


int main(){
    myFile file1;
    file1.open("/path/test.txt", "w+b");
    char fileInput[]={"a b c Test a b c\n"};
    file1.write(fileInput);
    printf("%i : %s\n",sizeof(fileInput),fileInput);
    //fwrite(fIn, sizeof(fIn[0]), sizeof(fIn)/sizeof(fIn[0]), file1.file);
    //fprintf(file1.file,"a b c d Test a b c d\n");
    fclose(file1.file);
    return 0;
}

When I try pass the string I want to write to the file (fileInput) to file1.write(), it appears to work, but the file that it writes to only contains the first 8 characters of fileInput.

The printf's placed for debug purposes to show the size and content of out in write(), and fileInput which is passed to it:

8 : a b c Test a b c

18 : a b c Test a b c

It becomes apparent that out is smaller than fileInput, but contains the same contents(?) which is confusing, as I assumed out would be treated as the actual argument passed to write() as out is a pointer to fileInput.

Is there any way I can make out be interpreted exactly the same as fileInput, or am I going about this the completely wrong way?

When I try pass the string I want to write to the file (fileInput) to file1.write(), it appears to work, but the file that it writes to only contains the first 8 characters of fileInput.

This is because of this:

write(in, sizeof(in[0]), sizeof(in)/sizeof(in[0]), file);

If we also look at the function header:

void write(char *in)

in has a type of char*. Thus sizeof(in) will return the size of a pointer (probably 8). in[0] has a type of char thus sizeof(in[0]) will return 1.

So this line will write 8 characters from the input.

What you need to do is pass the size of the string as a paramater. Or pass an object that has a size method built in. I would use std::string as the parameter (or mabe std::vector depending on usage).

void write(std::string const& in) {
    fwrite(&in[0], 1, in.size(), file);
}

Usage now becomes:

file1.write("hi there this is a message");

When passing your char-array to the write-method, it gets passed as a char-pointer. The sizeof-operator does NOT give you the length of the passed string. Instead use a function like strlen or pass the length of your string as additional parameter.

An even better solution would be to use std::string instead of char-arrays to pass strings.

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