?
Solved

FILE

Posted on 2003-03-09
9
Medium Priority
?
202 Views
Last Modified: 2010-04-01
Is there any possibility to split these codes into 4 functions: openFile, readFile, writeFile, closeFile?

FILE *fc;
fc=fopen("customer.txt", "a");

fprintf(fc, "%d\n%s\n%s\n%s\n", no, name, address, status);
while(fscanf(fc, "%d\n%[^\n]\n%[^\n]\n%[^\n]\n", &no, name, address, status)!=EOF){
 ***
 ***
 ***}
fclose(fc);
0
Comment
Question by:ami0714
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 8097340
You already are using 4 functions !

This is your "openFile" function call:
fopen("customer.txt", "a");

This is your "readFile" function call:
fscanf(fc, "%d\n%[^\n]\n%[^\n]\n%[^\n]\n", &no, name, address, status)

This is your "writeFile" function call:
fprintf(fc, "%d\n%s\n%s\n%s\n", no, name, address, status);

And this is your "closeFile" function call:
fclose(fc);

If you want to use your function names you can use wrappers, in example openFile will call fopen etc.
0
 

Author Comment

by:ami0714
ID: 8097495
I know you mean like this,

void openFile(){
fopen("customer.txt", "a");
}

void readFile(){
fscanf(fc, "%d\n%[^\n]\n%[^\n]\n%[^\n]\n", &no, name, address, status)
}

void writeFile(){
fprintf(fc, "%d\n%s\n%s\n%s\n", no, name, address, status);
}

void closeFile(){
fclose(fc);
}

but it is not working.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8097657
have u ensured that ur variables are accessible to the respective functions..
it seems u may like to have all the variables defined global,
so that all of ur functions can access them

FILE *fc;
int no;
char name[SIZE],address[SIZE],status[SIZE];


make these declarations as global.. OR define a struct to hold such variables and declare a global instance of that struct.

OR  make them all as private variables of a class and the functions as member functions of the class..

so many ways to do .. try the global variables.. its easy for ur example
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mafalda
ID: 8097726
No I didnt ment like that !

1) It doesn't work because you need to declare all the variables global in order for it to work.

2) I think that it is not necesary as the functions already exsits ... with other names.

3) If you insist, then if you want to wrap these functions in a way that you will not have to change your program, and not be forced to use globals you have to wrap them using the same signature.
Example:

As the signature of fclose is
int fclose( FILE *stream );
You need to have a wrapper as follows:

int closeFile(FILE *stream)
{
  return fclose(stream);
}

For variable-length argument list implementation read The C Programing Language SE by Kerigham & Ritchie page 155 section 7.3.

To make it simple:

int writeFile(FILE *stream, char* format, int no, char * name, char* address, char * status)
{
  return fprintf(stream, format, no, name, address, status);
}

pay attention that we make it simple as the call to writeFile is with a fix number of variables of a known type passed in the format variable.

I hope this is good enough.
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 8097727
sorry for the english:
"I didn't mean"
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 80 total points
ID: 8100965
Make a class:

class FileReader
{
  FILE * fc ;
public:
  void open_file ()
  {
    fc = fopen ( "Customer.txt", "a" ) ;

  } // end of open_file ()

  void read_file ()
  {
    fscanf ( fc, "%d\n%[^\n]\n%[^\n]\n%[^\n]\n", &no, name, address, status ) ;

  } // end of read_file ()

  void write_to_file ()
  {
    fprintf ( fc, "%d\n%s\n%s\n%s\n", no, name, address, status ) ;

  } // end of write_to_file ()

  void close_file ()
  {
    fclose ( fc ) ;

  } // end of close_file ()

} ; // end of class

void main ()
{
  FileReader obj ;
  obj.open_fle () ;
  obj.read_file () ;
  obj.write_to_file () ;
  obj.close_file () ;

} // end of main ()


One word of advice: You're using C++, fellow! Make us of fstream.h for file-handling, not this C file-pointer method!

Mayank.

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8101456
if he asked the query in c++ it doesnt mean he is doing c++ coding ..
btw he shud specify and post the query in appropriate Topic Area only

lets hear what ami0714 has to say
0
 

Author Comment

by:ami0714
ID: 8101699
I learn the pointer method but never good at it. That's why I'm still having some errors with Mafalda's suggestion.

Make a class...I never try this method but it's worth a try.

Btw, I'm doing a C++ program.
0
 

Author Comment

by:ami0714
ID: 8101787
wow, it works! Thank you Mayank. And Mafalda, I appreciate your effort in helping me too.

ami
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question