C language Program read and write files (file memory a decimal number, each read the value plus one)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.