C standard I/O build a file repository

Source: Internet
Author: User
Tags rewind

Realized the deletion and modification

A struct was created to save Num name cout price

Code

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>

typedef struct
{
int num;
Char name[32];
int count;
int price;
}good;

int main (int argc,char *argv[])
{
FILE *FP;
Good good;
Fp=fopen (Argv[1], "A +");
if (NULL==FP)
{
Perror ("Fopen1");
return-1;
}
Fclose (FP);
Fp=fopen (Argv[1], "r+");
if (NULL==FP)
{
Perror ("Fopen2");
return-1;
}
int n;
printf ("Please input your select function\n\n");
printf ("----1-------------store good\n");
printf ("----2------------browse good\n");
printf ("----3--------------seek good\n");
printf ("----4-------------alter good\n");
printf ("----5------------Delete good\n");
printf ("----6--------------rank good\n");
printf ("----7------------exit system\n");
while (scanf ("%d", &n) ==1)
{
GetChar ();
if (n==1)
{
printf ("Please input want store good ' num name Count price\n");
scanf ("%d%s%d%d", &good.num,good.name,&good.count,&good.price);
fprintf (FP, "%5d%16s%5d%5d\n", Good.num,good.name,good.count,good.price);
printf ("Store success\n");
}
else if (n==2)
{
int flag;
Rewind (FP);
while (1)
{
FLAG=FSCANF (FP, "%d%s%d%d\n", &good.num,good.name,&good.count,&good.price);
if ( -1==flag| | 0==flag)
{
printf ("\nlist above is all good\n\n\n");
Break
}
printf ("%5d%16s%5d%5d\n", Good.num,good.name,good.count,good.price);
}
}
else if (n==3)
{
Char str[32];
int find=0;
int flag;
Rewind (FP);
printf ("Please input want seek good name\n");
scanf ("%s", str);
GetChar ();
while (1)
{
FLAG=FSCANF (FP, "%d%s%d%d\n", &good.num,good.name,&good.count,&good.price);
if ( -1==flag| | 0==flag)
{
if (find==0)
printf ("\ni am sorry not having you want seek good\n\n\n");
Else
printf ("\nabove list is your want seek all good\n\n");
Break
}
if (strcmp (str,good.name) ==0)
{
find++;
printf ("\n%5d%16s%5d%5d\n", Good.num,good.name,good.count,good.price);
}
}
}
else if (n==4)
{
int number;
int flag;
Rewind (FP);
printf ("Please input want alter good ' s num\n");
scanf ("%d", &number);
GetChar ();
while (1)
{
int place= (int) ftell (FP);
FLAG=FSCANF (FP, "%d%s%d%d\n", &good.num,good.name,&good.count,&good.price);
if ( -1==flag| | 0==flag)
{
printf ("Not having fine you want alter num\n");
Break
}
if (number==good.num)
{
printf ("Please input altered ' information\n");
scanf ("%d%s%d%d", &good.num,good.name,&good.count,&good.price);
Fseek (Fp,place,seek_set);
fprintf (FP, "%5d%16s%5d%5d\n", Good.num,good.name,good.count,good.price);
printf ("Alter success!\n");
Break
}
}

}
Else if (n==5)
{
int success=0;
int number;
int place_1;
int flag;
Rewind (FP);
printf (" Please input want delete good ' s num\n ");
scanf ("%d", &number);
GetChar ();
while (1)
{
int place= (int) ftell (FP);
flag=fscanf (FP, "%d%s%d%d\n",&good.num,good.name,& Good.count,&good.price);
If ( -1==flag| | 0==flag)
{
//printf ("Not having fine you want delete num\n");
Fseek (Fp,place_1,seek_set);
fprintf (FP, "%c", EOF);
printf ("delete success\n!");
break;
}
if (success)
{
fseek (fp,place_1,seek_set);
fprintf (FP,%5d%16s%5d%5d\n, Good.num,good.name, Good.count,good.price);
}
if (number==good.num)
{
Success=1;
}
Place_1=place;
}

}
else if (n==6)
{

}
else if (n==7)
{
Fclose (FP);
printf ("You exit success Welcome come again\n");
Break
}
Else
{
printf ("Your input error please input again\n\n");
}
printf ("Please input your select function\n\n");
printf ("----1-------------store good\n");
printf ("----2------------browse good\n");
printf ("----3--------------seek good\n");
printf ("----4-------------alter good\n");
printf ("----5------------Delete good\n");
printf ("----6--------------rank good\n");
printf ("----7------------exit system\n");

}
return 0;
}

C standard I/O build a file repository

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.