[英]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. 我是C / C ++编程的新手,我正在努力提高对文件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).
有了这个程序,我最初试图使myFile成为C中的typedef(显然不起作用),所以我转移到C ++中的类(这就是为什么没有代码使用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. 当我尝试将要写入文件(fileInput)的字符串传递给file1.write()时,它似乎工作,但它写入的文件只包含fileInput的前8个字符。
The printf's placed for debug purposes to show the size and content of out in write(), and fileInput which is passed to it: printf用于调试目的,以显示write()中的out的大小和内容,以及传递给它的fileInput:
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. 很明显out小于fileInput,但包含相同的内容(?),这是令人困惑的,因为我假设将被视为传递给write()的实际参数,因为out是指向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? 有没有什么方法可以解释与fileInput完全相同,或者我是否以完全错误的方式解决这个问题?
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.
当我尝试将要写入文件(fileInput)的字符串传递给file1.write()时,它似乎工作,但它写入的文件只包含fileInput的前8个字符。
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*. in
有一种char *。 Thus sizeof(in)
will return the size of a pointer (probably 8). 因此
sizeof(in)
将返回指针的大小(可能为8)。 in[0]
has a type of char
thus sizeof(in[0])
will return 1. in[0]
有一个char
类型,因此sizeof(in[0])
将返回1。
So this line will write 8 characters from the input. 所以这一行将从输入中写入8个字符。
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).
或者传递一个内置了size方法的对象。我会使用std :: string作为参数(或mabe std :: vector,具体取决于用法)。
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. 将char-array传递给write-method时,它将作为char-pointer传递。 The sizeof-operator does NOT give you the length of the passed string.
sizeof-operator没有给出传递字符串的长度。 Instead use a function like strlen or pass the length of your string as additional parameter.
而是使用像strlen这样的函数或将字符串的长度作为附加参数传递。
An even better solution would be to use std::string instead of char-arrays to pass strings. 更好的解决方案是使用std :: string而不是char-arrays来传递字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.