• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

FILE

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
ami0714
Asked:
ami0714
  • 3
  • 3
  • 2
  • +1
1 Solution
 
MafaldaCommented:
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
 
ami0714Author Commented:
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
 
akshayxxCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MafaldaCommented:
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
 
MafaldaCommented:
sorry for the english:
"I didn't mean"
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
akshayxxCommented:
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
 
ami0714Author Commented:
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
 
ami0714Author Commented:
wow, it works! Thank you Mayank. And Mafalda, I appreciate your effort in helping me too.

ami
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now