1. Problem: C language Program to read and write a txt file, txt file store a decimal number, each read the value plus one.
2. Implement: Create a new folder, build a OutputFileName.txt file in the folder, the content is: 1, and then create a new t.c file in the folder, the content is the following code.
3. Code:
#include <stdio.h>
#include <stdlib.h>
char * getfilenum ();
void Setfilenum (char * filenume);
int main () {
char * number=getfilenum ();//Get File Number
printf ("test%s\n", number);
Setfilenum (number);//Set file numbers plus one
return 0;
}
How to get the file number
char * Getfilenum () {
Char number[5];
File *fp=fopen ("OutputFileName.txt", "R");//open files in a readable manner to get the filename number to create
FSCANF (FP, "%s", number);//Read
Fclose (FP); Close File
Char *filenum= (char *) malloc (sizeof (char) *6);
int i=0;
while (number[i]!= ')
{
* (Filenum+i) =number[i];
i++;
}
* (filenum+i) = ' + ';
return filenum;
}
How to set the file number plus one
void Setfilenum (char * filenume) {
File *fp=fopen ("OutputFileName.txt", "w");//open files in a writable manner, write the next file name number to be created
int Numberint=atoi (filenume);//Convert the file name number of the read character type to int type
numberint++;//file name number plus one
Itoa (numberint,filenume,10);//Convert the file number of the int type to a character type
Fputs (FILENUME,FP);//writes the file name number to be created the next time
Fclose (FP);//Close file
}
C language Program read and write files (file memory a decimal number, each read the value plus one)