21xrx.com
2024-09-19 09:40:53 Thursday
登录
文章检索 我的文章 写文章
C++如何调用外部文件?
2023-06-22 03:51:47 深夜i     --     --
C++ 外部文件 调用

在C++中,调用外部文件(如文本文件、字体、图片等)是非常普遍的操作。下面介绍几种常用的方法。

1. 使用fstream类读写文件

fstream是C++标准库提供的文件流类,可以用于读写文件。使用fstream类打开文件时需要指定文件名和打开模式,常见的打开模式包括:

· ios::in,打开文件进行读取操作;

· ios::out,打开文件进行写操作;

· ios::app,打开文件进行追加操作;

· ios::binary,以二进制方式打开文件。

以下是一个示例程序,读取一个文本文件中的内容并输出到控制台:

#include

#include

using namespace std;

int main () {

 string line;

 ifstream myfile ("example.txt");

 if (myfile.is_open()) {

  while (getline(myfile,line)) {

   cout << line << '\n';

  }

  myfile.close();

 }

 else cout << "Unable to open file";

 return 0;

}

这个程序先用ifstream类打开example.txt文件,如果成功打开,则逐行读取文件内容并输出到控制台。注意,需要使用is_open()方法检查文件是否打开成功,避免读取错误导致程序崩溃。

2. 使用stdio.h库函数读写文件

除了fstream类,C++还可以使用标准C库函数读写文件。其中,fopen函数用于打开文件,fgets函数用于读取一行文本,fscanf函数用于读取指定格式的文本,fwrite函数用于写入二进制数据。

以下是一个示例程序,将文本文件中的内容复制到另一个文件中保存:

#include

int main () {

 FILE *fptr1, *fptr2;

 char filename[100], c;

 printf("Enter the filename to open for reading: ");

 scanf("%s", filename);

 // Open one file for reading

 fptr1 = fopen(filename, "r");

 if (fptr1 == NULL) {

  printf("Cannot open file %s \n", filename);

  exit(0);

 }

 printf("Enter the filename to open for writing: ");

 scanf("%s", filename);

 // Open another file for writing

 fptr2 = fopen(filename, "w");

 if (fptr2 == NULL) {

  printf("Cannot open file %s \n", filename);

  exit(0);

 }

 // Read contents from file

 c = fgetc(fptr1);

 while (c != EOF) {

  fputc(c, fptr2);

  c = fgetc(fptr1);

 }

 printf("Contents copied to %s", filename);

 fclose(fptr1);

 fclose(fptr2);

 return 0;

}

3. 使用第三方库载入特定格式文件

如果需要读取特定格式的文件,可以使用第三方库。比如,libpng库可以读取PNG格式的图片,libxml2库可以读取XML格式的文本。

以下是一个使用libpng库读取PNG文件并显示图片的示例程序:

#include

void read_png_file(char *filename) {

 // Open file

 FILE *fp = fopen(filename, "rb");

 if (!fp)

  abort();

 // Read header

 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

 if (!png_ptr)

  abort();

 png_infop info_ptr = png_create_info_struct(png_ptr);

 if (!info_ptr)

  abort();

 if (setjmp(png_jmpbuf(png_ptr)))

  abort();

 png_init_io(png_ptr, fp);

 png_read_info(png_ptr, info_ptr);

 // Read image

 if (setjmp(png_jmpbuf(png_ptr)))

  abort();

 int width = png_get_image_width(png_ptr, info_ptr);

 int height = png_get_image_height(png_ptr, info_ptr);

 png_byte color_type = png_get_color_type(png_ptr, info_ptr);

 png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);

 if (bit_depth == 16)

  png_set_strip_16(png_ptr);

 if (color_type == PNG_COLOR_TYPE_PALETTE)

  png_set_palette_to_rgb(png_ptr);

 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)

  png_set_expand_gray_1_2_4_to_8(png_ptr);

 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))

  png_set_tRNS_to_alpha(png_ptr);

 if (color_type == PNG_COLOR_TYPE_RGB ||

   color_type == PNG_COLOR_TYPE_GRAY ||

   color_type == PNG_COLOR_TYPE_PALETTE)

  png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);

 if (color_type == PNG_COLOR_TYPE_GRAY ||

   color_type == PNG_COLOR_TYPE_GRAY_ALPHA)

  png_set_gray_to_rgb(png_ptr);

 png_read_update_info(png_ptr, info_ptr);

 if (setjmp(png_jmpbuf(png_ptr)))

  abort();

 png_bytep *row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);

 for (int y=0; y

  row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));

 png_read_image(png_ptr, row_pointers);

 // Display image

 for (int y=0; y

  for (int x=0; x

   png_bytep px = &(row_pointers[y][x*4]);

   printf("Pixel at position [%d - %d] has RGBA values: %d - %d - %d - %d\n",

      x, y, px[0], px[1], px[2], px[3]);

  }

 }

 // Clean up

 for (int y=0; y

  free(row_pointers[y]);

 free(row_pointers);

 fclose(fp);

}

int main() {

 read_png_file("image.png");

 return 0;

}

这里用png.h头文件中的png_*函数读取PNG格式的图片,先读取图片的宽度、高度、颜色类型和色深等信息,然后按字节读取像素值并输出到控制台。注意,读取PNG文件时需要安装libpng库。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章