Hi, I m trying to convert c code to c++. I m stuck at some part! Please help.

What will be c++ equivalent of this snippet?

void Scanned(FILE * ifp,int i)
void Read(FILE * ofp)
int  Checker(char s[],char u[])
void Stored(FILE *)
void Printed(FILE *ifp,FILE *ofp)

These are file pointers as parameters.

Also, is this struct correct for c++?

typedef struct checker
  int age;
  char name[20];
  int  roll_call;

Recommended Answers

All 11 Replies

What will be c++ equivalent of this snippet?

There would be no differences (barring the obvious syntax errors, but that would be broken in both languages). The FILE structure is a standard type inherited from C.

Also, is this struct correct for c++?


You are missing the ; off your function prototypes. The ones you have, should work with your compiler..

You could, however, do the following:

void Scanned(std::ifstream& ifp,int i);

but make sure you include <fstream>

Your struct seems fine, is there a reason why you're not using classes - Just out of interest?

You shouldn't really need the typedef and would be just the following:

struct Checker
  int age;
  char name[20];
  int  roll_call;

Also, in the following: Checker pass in the arrays as references: int Checker(char *s,char *u)

Hope this helps.

@deceptikon and @ phorce Thankyou!
@phorce why'd you say pass array as references in Checker function? why not by value?

@Andy - If we pass by reference, we get the actual memory allocations and not the value. Thus meaning we can ensure speed as well as the fact if the values have to be changed in any way.. Then this will be done in direct memory.

If this problem has been solved. Could you mark it as solved please?

Give me suggestion on how can I solve this by using Classes?

Here it is as a class

class foo
    void Scanned(ifstrem& ifp,int i);
    void Read(ofstream& ofp);
    int  Checker(std::string s,std::string u);
    void Stored(fstream&);
    void Printed(ifstream& ifp,ofstream& ofp);

Can I use

int Checker(char s[], char u[]);

instead of

int Checker(std::string s,std::string u);

pass in the arrays as references: int Checker(char *s,char *u)

The signatures int Checker(char s[], char u[]) and int Checker(char *s, char *u) are 100% equivalent. It is not possible to pass C arrays by value.

Also note that s and u are pointers, not references. A reference to an array would look like int (&s)[N], but that construct is not commonly used.

fprintf(ifp,"%d %-20s %d \n",i->id,i->word,i->count);

How can I write this using fputs?
ifp is filepointer.

You can't, at least not in just one line. printf() does all the formatting for you. If you want to use fputs() then you will have to do all the formatting yourself. Much simpler to just use fprintf()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.