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;
}inti;

Recommended Answers

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

Yes.

Jump to Post

All 11 Replies

Re: Conversion of C code to C++ 80 80

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

Yes.

Re: Conversion of C code to C++ 80 80

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.

Re: Conversion of C code to C++ 80 80

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

Re: Conversion of C code to C++ 80 80

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

Re: Conversion of C code to C++ 80 80

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

Re: Conversion of C code to C++ 80 80

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

Re: Conversion of C code to C++ 80 80

Here it is as a class

class foo
{
public:
    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);
};
Re: Conversion of C code to C++ 80 80

Can I use

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

instead of

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

Re: Conversion of C code to C++ 80 80

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.

Re: Conversion of C code to C++ 80 80

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

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

Re: Conversion of C code to C++ 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.