Skip to content

Instantly share code, notes, and snippets.

@pawitp
Created October 6, 2011 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawitp/1267331 to your computer and use it in GitHub Desktop.
Save pawitp/1267331 to your computer and use it in GitHub Desktop.
128 Pawit 18 175.5 N
12 Phruksa 18 181.5 N
void main()
{
FILE *fid;
int i, count;
long id[100];
char fname[100][20];
int age[100];
double height[100];
char married[100][2];
fid = fopen("friends.txt","r");
if(!(fid==NULL))
{
i=0;
while(!feof(fid))
{
fscanf(fid,"%ld%s%d%lf%s\n",&id[i],&fname[i],&age[i],&height[i],&married[i]);
i++;
}
count=i;
fclose(fid);
printf("%-10s%-20s%-4s%-10s%-10s\n","ID","Name","Age","Height","Married");
// Print
for(i = 0; i < count; i++)
{
printf("%-10ld%-20s%-4d%-10.1lf%-10s\n",id[i],fname[i],age[i],height[i],married[i]);
}
// Update age of Pawit
age[0] = 20;
// Update name of Phruksa
strcpy(fname[1], "Kularb");
// Add new record
id[count] = 100;
strcpy(fname[count], "Thanat");
age[count] = 19;
height[count] = 190;
strcpy(married[count], "Y");
count = count + 1;
// Write to file
fid = fopen("D:\\friends.txt", "w");
for (i = 0; i < count; i++) {
fprintf(fid, "%-10ld%-20s%-4d%-6.1lf%1s\n", id[i], fname[i], age[i], height[i], married[i]);
}
fclose(fid);
}
else printf("Cannot open file.");
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment