The descripetion of my problem:
Segmentation fault (core dumped)
The corresponding codes:
#include <iostream>
#include <stdio.h>
#include <cstring>
int main() {
//build a directory named data in first level
/*std::string path = ".../data/";
std::string mkdir_command = "mkdir -p ";
std::string dir_command = mkdir_command + path;
system(dir_command.c_str());*/
//write 2 and 3 into a file named test_data
std::string path = ".../data/";
path = path + "test_data";
FILE *fp = fopen(path.c_str(), "w+");
int x = 2, y = 3;
unsigned char buffer_in[sizeof(int)];
memcpy(buffer_in, &x, sizeof(int));
fwrite(&buffer_in, sizeof(int), 1, fp);
memcpy(buffer_in, &y, sizeof(int));
fwrite(buffer_in, sizeof(int), 1, fp);
fclose(fp);
//read 3 from a file named test_data
fp = fopen(path.c_str(), "r");
fp += sizeof(int);
fread(buffer_in, sizeof(int), 1, fp);
memcpy(&x, buffer_in, sizeof(int));
fclose(fp);
//show the value read from file test_data
printf("%d", x);
return 0;
}
The above mistakes contain:
".../data/"
should be `"…/data/"
fp = fopen(path.c_str(), "r");
fp += sizeof(int);
fread(buffer_in, sizeof(int), 1, fp);
memcpy(&x, buffer_in, sizeof(int));
fclose(fp);
The next I just change the framework a little:
#include <iostream>
#include <stdio.h>
#include <cstring>
int main() {
//build a directory named data in first level
int timestamp = time(0) % 1000+1;
std::string path = "data/";
std::string mkdir_command = "mkdir -p ";
std::string dir_command = mkdir_command + path;
system(dir_command.c_str());
//write 2 and 3 into a file named test_data
path = path + "test_data" + std::to_string(timestamp);
FILE *fp = fopen(path.c_str(), "wb+");
int x = 2, y = 3;
unsigned char buffer_in[sizeof(int)];
memcpy(buffer_in, &x, sizeof(int));
fwrite(&buffer_in, sizeof(int), 1, fp);
memcpy(buffer_in, &y, sizeof(int));
fwrite(buffer_in, sizeof(int), 1, fp);
fclose(fp);
//read 3 from a file named test_data
FILE *f = NULL;
f = fopen(path.c_str(), "rb");
fread(buffer_in, sizeof(int), 1, fp);
memcpy(&y, buffer_in, sizeof(int));
fclose(f);
std::cout << y << std::endl;
return 0;
}
The above results:
2
finally I just find the mistake source:
fread(buffer_in, sizeof(int), 1, fp + sizeof(int));
When I just change the code in line 25, the following mistakes appears:
Segmentation fault (core dumped)
which means the pointer in type of FILE cannot add some value directly.
The solution for above problem:
int fseek(FILE *stream, long int offset, int whence)
Therefore, my corect codes is followings:
#include <iostream>
#include <stdio.h>
#include <cstring>
int main() {
//build a directory named data in first level
int timestamp = time(0) % 1000+1;
std::string path = "data/";
std::string mkdir_command = "mkdir -p ";
std::string dir_command = mkdir_command + path;
system(dir_command.c_str());
//write 2 and 3 into a file named test_data
path = path + "test_data" + std::to_string(timestamp);
FILE *fp = fopen(path.c_str(), "wb+");
int x = 2, y = 3;
unsigned char buffer_in[sizeof(int)];
memcpy(buffer_in, &x, sizeof(int));
fwrite(&buffer_in, sizeof(int), 1, fp);
memcpy(buffer_in, &y, sizeof(int));
fwrite(buffer_in, sizeof(int), 1, fp);
fclose(fp);
//read 3 from a file named test_data
fp = fopen(path.c_str(), "rb");
if((fseek(fp, sizeof(int), SEEK_SET)!=0)){
std::cout << "The file pointer reset failed!" << std::endl;
}
fread(buffer_in, sizeof(int), 1, fp);
memcpy(&x, buffer_in, sizeof(int));
fclose(fp);
std::cout << "The value of x = "<< x << std::endl;
return 0;
}
The corresponding results:
The value of x = 3