CARI Infonet

 Forgot password?
 Register

ADVERTISEMENT

View: 3411|Reply: 6

NEED HELP IN C PROGRAMMING!!

[Copy link]
Post time 12-3-2008 12:05 PM | Show all posts |Read mode
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!
Reply

Use magic Report


ADVERTISEMENT


Post time 12-3-2008 12:48 PM | Show all posts

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 ?
Reply

Use magic Report

 Author| Post time 12-3-2008 12:52 PM | Show all posts

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
Reply

Use magic Report

 Author| Post time 12-3-2008 12:58 PM | Show all posts
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;
}
Reply

Use magic Report

Post time 13-3-2008 08:29 AM | Show all posts

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.
Reply

Use magic Report

Post time 19-3-2008 08:26 PM | Show all posts
contoh kat intenet byk tuh.. cuba tanya pakcik google
Reply

Use magic Report

Follow Us
Post time 17-9-2010 11:22 PM | Show all posts

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>

  4. struct a //a struct with 2 declared variables
  5. {
  6.         char name[20];
  7.         int age;
  8. };

  9. void input(a &orang)
  10. {
  11.         cout<<"Enter name : ";
  12.         cin.getline(orang.name,256);
  13.         cout<<"Enter age : ";
  14.         cin>>orang.age;       
  15. }

  16. void write(char* a, int b)
  17. {
  18.         char* filename = "test.txt";
  19.         ofstream myfail;
  20.         myfail.open (filename);
  21.         if (myfail.fail())
  22.         {
  23.                 cerr<<"Error open file";
  24.                 exit(-1);
  25.         }
  26.         myfail<<a<<endl;
  27.         myfail<<b<<endl;
  28.         myfail.close();
  29.         cout<<"\nSuccessful write to "<<filename<<endl;
  30. }

  31. void read()
  32. {
  33.         fstream test;
  34.         char aksara;
  35.         cout<<"\nRead Data :"<<endl;
  36.         test.open ("test.txt",ios::in);
  37.         if (test.fail())
  38.         {
  39.                 cerr<<"Error open file";
  40.                 exit(-1);
  41.         }
  42.         test.get(aksara);
  43.         while(!test.eof()) {
  44.                 cout<<aksara;
  45.                 test.get(aksara);
  46.         }
  47.         test.close();
  48. }


  49. void main() //main function
  50. {
  51.         struct a orang;  //change the struct name from a to b
  52.         input(orang);
  53.         write(orang.name, orang.age);
  54.         read();
  55. }
Copy the Code
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CARI Infonet

30-4-2024 03:47 AM GMT+8 , Processed in 0.054488 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list