简体   繁体   中英

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...

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

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

use the filename as argumnent for your program. for example : check_location filename

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)

Below is the sudo code, considering you filename contains the file to open and write.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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