adziah Publish time 12-3-2008 12:05 PM

NEED HELP IN C PROGRAMMING!!

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' functionstruct a //a struct with 2 declared variables
{
char *name;
int age;
};int main() //main function
{
struct a b;//change the struct name from a to b
//b.name = "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!

hear2heart Publish time 12-3-2008 12:48 PM

Reply #1 adziah's post

dear adziah,

sy rasa problem awak dlm write function. first awak tak perlu variables bila call write function masa dlm main function. you don't have to give the structure to the write function. next is , masa you write the name and age data dlm structrue tu salah. sepatutnya "a.name" . lagi satu kenapa ada "return 0" dlm setiap function u ? awak guna compiler jenis apa ?

adziah Publish time 12-3-2008 12:52 PM

Reply #2 hear2heart's post

visual studio 6..

owh...tak perlu ke return 0?

ok ok saya cuba k..

kalau ok saya bagitau awak lagi

adziah Publish time 12-3-2008 12:58 PM

i found out that masalahnya berpunca dari sini..

        fprintf(fp,"%s",name);
        fprintf(fp,"%d",age);                //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;
}

hear2heart Publish time 13-3-2008 08:29 AM

Reply #4 adziah's post

dear adziah ,

" fprintf(fp,"%s",name);
      fprintf(fp,"%d",age);                //write the user input data into the test.txt file "

"name" tu awak kena gantikan dgn "a.name" dan "age" awak kena gantikan dgn "a.age" . both function read and write tu boleh buang int kat depan gantikan dgn void .

utk read function awk tu , boleh guna fscanf dan fprintf untuk replace getchar dan putchar.

liverpoolfctv Publish time 19-3-2008 08:26 PM

contoh kat intenet byk tuh.. cuba tanya pakcik google

otai_g Publish time 17-9-2010 11:22 PM


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

struct a //a struct with 2 declared variables
{
        char name;
        int age;
};

void input(a &orang)
{
        cout<<"Enter name : ";
        cin.getline(orang.name,256);
        cout<<"Enter age : ";
        cin>>orang.age;       
}

void write(char* a, int b)
{
        char* filename = "test.txt";
        ofstream myfail;
        myfail.open (filename);
        if (myfail.fail())
        {
                cerr<<"Error open file";
                exit(-1);
        }
        myfail<<a<<endl;
        myfail<<b<<endl;
        myfail.close();
        cout<<"\nSuccessful write to "<<filename<<endl;
}

void read()
{
        fstream test;
        char aksara;
        cout<<"\nRead Data :"<<endl;
        test.open ("test.txt",ios::in);
        if (test.fail())
        {
                cerr<<"Error open file";
                exit(-1);
        }
        test.get(aksara);
        while(!test.eof()) {
                cout<<aksara;
                test.get(aksara);
        }
        test.close();
}


void main() //main function
{
        struct a orang;//change the struct name from a to b
        input(orang);
        write(orang.name, orang.age);
        read();
}
Pages: [1]
View full version: NEED HELP IN C PROGRAMMING!!


ADVERTISEMENT