Você está na página 1de 4

can create new user

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
FILE *fptr; //file pointer
fptr = fopen("writeonly.txt","w"); //creates a file for writing.
//If file exists it will be deleted
printf("\nThe file was created");
fprintf(fptr,"%s","Hello");
printf("\nFile written to. Press any key to continue...");
getch();
}

********************************************************************************
************************************
Can find user file and can edit the user file
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
char fname[34];
char lname[25];
int main(void)
{
FILE *fptr; //file pointer
fptr = fopen("readwrite.txt","r+"); //opens a file for reading and writing
if(fptr == NULL)
printf("\nThe file was not found");
else
{
printf("\nThe file was found");
for(int c=0;c<3;c++)
{
printf("\nEnter First Name: ");
fflush(stdin);
gets(fname);
printf("\nEnter Last Name: ");
fflush(stdin);
gets(lname);
fprintf(fptr,"%s %s\n",fname,lname);
}
}

printf("\nPress any key to continue...");


getch();
}
********************************************************************************
*************************************
This is to view current bank member infomation
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
FILE *fptr; //file pointer
char data[100];
char c;
fptr = fopen("sequential2.txt","r+"); //opens a file for reading and writing
if(fptr == NULL)
printf("\nThe file was not found");
else
{
printf("\nThe file was found\n\n");
printf("\n\nThis was printed using FEOF\n");
rewind(fptr);//places the file pointer at the beginning of the file
while(!feof(fptr))
{
fscanf(fptr,"%s",data);
printf("%s ",data);
}
printf("\n\nThis was printed using FGETS\n");
rewind(fptr);//places the file pointer at the beginning of the file
if(fgets(data,85,fptr) != NULL)
printf("%s ",data);
printf("\n\nThis was printed using FGETC\n");
rewind(fptr);
c = fgetc(fptr);
while(!feof(fptr))
{
printf("%c",c);
c = fgetc(fptr);
}
}
printf("\nPress any key to continue...");
getch();
}
********************************************************************************
*************************************
Second option to save files

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MYSIZE 25
#define SIZE 3
typedef struct personal
{
char name[25];
char address[50];
int age;
}info;
void
void
void
void

openfile();
getinfo(info data[]);
printinfo(info data[]);
storeinfo(info data[]);

FILE *fptr;
int main(void)
{
info data[MYSIZE];
openfile();
getinfo(data);
printinfo(data);
storeinfo(data);
}
void openfile()
{
fptr = fopen("name.txt","a+"); //opens a file for reading only
if(fptr == NULL)
{
printf("\nThe
printf("\nPress
getch();
exit(1);
}
else
{
printf("\nThe
printf("\nPress
getch();
}

file was not found.");


any key to exit...");

file was found.");


any key to continue...");

}
void getinfo(info data[MYSIZE])
{
for(int count=0;count<SIZE;count++)
{
clrscr();
printf("\nEnter name: ");
fflush(stdin);
gets(data[count].name);

printf("\nEnter address: ");


fflush(stdin);
gets(data[count].address);
printf("\nEnter age: ");
fflush(stdin);
scanf("%d",&data[count].age);
}
}
void printinfo(info data[MYSIZE])
{
for(int count=0;count<SIZE;count++)
{
printf("\n[%s] [%s] [%i]",data[count].name,data[count].address,
data[count].age);
}
printf("\nPress any key to continue...");
getch();
}
void storeinfo(info data[MYSIZE])
{
for(int count=0;count<SIZE;count++)
{
fprintf(fptr,"%s %s %i\n",data[count].name,
data[count].address,
data[count].age);
}
}
********************************************************************************
*************************************

Você também pode gostar