A special mistake when using FILE(how to reset the file pointer)

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:

  1. ".../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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值