简体   繁体   English

如何在c中读取文件输入名称

[英]How to read file input name in c

I have a task that need to read the file name input and replace the content with the same name...here is the example..我有一个任务需要读取文件名输入并用相同的名称替换内容......这是示例..

Suppose i have program called check_location.c that read the input from barcodes.txt...i have to read the content and create a new file again called barcodes.txt...假设我有一个名为 check_location.c 的程序,它读取来自barcodes.txt 的输入……我必须读取内容并再次创建一个名为barcodes.txt 的新文件……

But the problem is, if the input file name is something.txt, my program will still work but it will create a file call barcodes.txt但问题是,如果输入文件名是 something.txt,我的程序仍然可以运行,但它会创建一个文件调用barcodes.txt

Please give me some solutions to solve this problem as it almost the due date...i have searched anywhere for hours but still could not find it....thankssssssss请给我一些解决方案来解决这个问题,因为它几乎是截止日期...我已经在任何地方搜索了几个小时但仍然找不到它....thankssssssss

use the filename as argumnent for your program.使用文件名作为程序的参数。 for example : check_location filename例如:check_location 文件名

You can use the argument to read the file and write the file.您可以使用参数来读取文件和写入文件。 If check_location does not contain your main function than use it as a argument in your function check_location(char* filename)如果 check_location 不包含您的主函数,则将其用作函数 check_location(char* filename) 中的参数

Below is the sudo code, considering you filename contains the file to open and write.下面是 sudo 代码,考虑到您的文件名包含要打开和写入的文件。

assuming you want to read from file, do some operations and write output to the same file with new contents (overwrite the existing file)假设您想从文件中读取,执行一些操作并将输出写入具有新内容的同一个文件(覆盖现有文件)

char filename[16] = "barcodes.txt"
fd = fopen(filename, "r");

// do your operations

fclose(fd);

fd = fopen(filename, "w+");

// do write to file

fclose(fd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM