Created
October 6, 2011 12:57
-
-
Save pawitp/1267331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 Pawit 18 175.5 N | |
12 Phruksa 18 181.5 N |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
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