i'm really desperate in doing this thing..
dah banyak orang aku tanya termasuk lecturer sendiri tapi dia suruh aku cari solution sendiri..
requirements soalan tu macam ni...
1. Create a structure with at least 2 variables 2. Write a function to input data to the structure created in question 1 3. Write a function to write the content from question 2 to text file 4. Write a function to read back from the text file from question 3 aku dah buat dah program tu tapi tak menjadi plak..tak tau apa prob dia..leh tak tolong aku?? #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int read(); //declaration for 'read' function
int write(char *a, int b); //declaration for 'write' function struct a //a struct with 2 declared variables
{
char *name[20];
int age;
}; int main() //main function
{
struct a b; //change the struct name from a to b
//b.name[20] = "Azie";
//b.age=24; //setting the value for name and age
write(b.name, b.age); //calling the 'write' function
read(b.name, b.age); //calling the 'read' function
return 0;
} int write(char *a, int b)
{
FILE *f;
f = fopen("C:\Documents and Settings\cs2082\Desktop\test.txt","w"); //open test.txt and create if it doesn't exist
printf("Enter a name: ");
scanf("%s",a);
printf("Enter the age: ");
scanf("%d",b);
fprintf(f,"%s",a);
fprintf(f,"%d",b); //write the user input data into the test.txt file
return 0;
} int read()
{
char c;
FILE *f;
f = fopen("test.txt","r"); //open and read the test.txt file
while((c=getchar()) !=EOF)
{
putchar(c);
}
return 0;
} THANKS! |