Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,835 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,717 people online right now. Registration is fast and FREE... Join Now!




C to C++

 
Reply to this topicStart new topic

C to C++, C to C++

markorulz1
20 Sep, 2008 - 01:53 PM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2008
Posts: 4

can somebody translate me this to C++, coz i didnt understand many commands here in C, i;m learning only C++,Thanks

cpp

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int state;
char buffer[100];
FILE *fin, *fout;
int ch;
int br = 0;

if(argc != 2)
{
printf("usage: %s <file input>\n", argv[0]);
exit(1);
}

fin = fopen(argv[1], "r");
if(fin == NULL)
{
perror(" ");
exit(1);
}

state = 0;
while(( ch = fgetc(fin)) != EOF)
{
if(state == 0)
{
sprintf(buffer, "%d", br);
strcat(buffer,".txt");
fout = fopen(buffer, "w");
if(fout == NULL)
{
perror("");
exit(1);
}
br++;
state = 0;
}
else
if(state == 120)
{
state = 0;
fputc(ch,fout);
fclose(fout);
continue;

}
if(ch == '\n')
state++;

fputc(ch,fout);
}

printf("\nExiting...\n");
return 0;
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 20 Sep, 2008 - 01:56 PM
User is offlineProfile CardPM
+Quote Post

joske
RE: C To C++
21 Sep, 2008 - 11:24 PM
Post #2

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
Well, actually C++ is compatible with C (roughly said: C++ is C with OOP added). A C program will compile fine in C++, though not the other way around. So: you can directly compile this C program in your C++ compiler.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: C To C++
22 Sep, 2008 - 03:11 AM
Post #3

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 597



Thanked: 60 times
Dream Kudos: 50
My Contributions
As Joske said, there's no reason to translate this (unless it's homework, in which case we'd be aiding and abetting you in cheating).

Hint:
cpp
sprintf(buffer, "%d", br);
strcat(buffer,".txt");


could just be
cpp
sprintf(buffer, "%d.txt", br);

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: C To C++
22 Sep, 2008 - 03:59 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,551



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
If it has to be pure C++ you'll have to use fstream for the files.
User is offlineProfile CardPM
+Quote Post

David W
RE: C To C++
22 Sep, 2008 - 04:30 AM
Post #5

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
QUOTE(markorulz1 @ 20 Sep, 2008 - 02:53 PM) *

can somebody translate me this to C++, coz i didnt understand many commands here in C, i;m learning only C++,Thanks

cpp

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int state;
char buffer[100];
FILE *fin, *fout;
int ch;
int br = 0;

if(argc != 2)
{
printf("usage: %s <file input>\n", argv[0]);
exit(1);
}

fin = fopen(argv[1], "r");
if(fin == NULL)
{
perror(" ");
exit(1);
}

state = 0;
while(( ch = fgetc(fin)) != EOF)
{
if(state == 0)
{
sprintf(buffer, "%d", br);
strcat(buffer,".txt");
fout = fopen(buffer, "w");
if(fout == NULL)
{
perror("");
exit(1);
}
br++;
state = 0;
}
else
if(state == 120)
{
state = 0;
fputc(ch,fout);
fclose(fout);
continue;

}
if(ch == '\n')
state++;

fputc(ch,fout);
}

printf("\nExiting...\n");
return 0;
}

*edit: Please use code tags in the future, thanks! code.gif


What do you want your program to do?

1. Get the file name to read from the command line?

theProgram.exe fileToRead.dat

2. Read a file? (and check that it opened ok?)

3. Write some data that was read to a new file?

These C/C++ functions are all available.

Look for examples of each in your text or on the Web.

Shalom,

David

P.S.

Think of the steps, as per above, that you want to do, then write the code for each step, and check that that part is working, before you add more code.

CODE

// writing/reading a text file
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char inFileName[]= "example.txt";

int main ()
{
    // fstream myfile("example.txt", ios::in|ios:out); // fstream default mode
    fstream myfile(inFileName); // <-- fstream default mode
    if (myfile.is_open())
    {
        myfile << "This is a line.\n";
        myfile << "This is another line.\n";
        myfile.close();
    }
    else
        cout << "Unable to open file";
    
    myfile.open(inFileName);
    string line;
    
    while( getline( myfile, line ) )
        cout << line << endl;
    
    myfile.close();
    
    cout << "Press 'Enter' to exit ... ";
    cin.get();
    return 0;
}



User is offlineProfile CardPM
+Quote Post

baavgai
RE: C To C++
22 Sep, 2008 - 05:18 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Well, C is available in C++ so you're set. However, your program itself seems to have a number of bugs, if I'm interpreting it's intent correctly.

Here's a fixed version of the C program with copious comments. Hopefully this will help you write a C++ style version.

cpp

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int state; // counter, state isn't a great name
char buffer[100]; // scratch area for strings
FILE *fin, *fout; // file handles
int ch; // holds the character read
int br = 0; // file name counter, br seems meaningless

if(argc != 2) {
printf("usage: %s <file input>\n", argv[0]);
return 1;
}

fin = fopen(argv[1], "r"); // open file name passed to program for reading
if(fin == NULL) { return 1; } // fail out if file not opened

state = -1; // init state, not zero, explained later
while(( ch = fgetc(fin)) != EOF) { // read one char at a time, until end of file
if(state == 120) { // if we're read 120 lines, time to start a new file
fclose(fout);
state = -1;
}

if(state == -1) { // new file time
sprintf(buffer, "%d.txt", br++); // file name, 0.txt, 1.txt, etc.
fout = fopen(buffer, "w"); // open file for writing
if(fout == NULL) { return 1; } // fail out if can't open
state = 0; // important, otherwise state -1 will exist until first new line
// now we're starting on line 0 our count will work
}

if(ch == '\n') { state++; } // when an end of line is hit, count it
fputc(ch,fout); // write the character we read to the output file
}

fclose(fout); // important, final file close, this was not in original program
fclose(fin); // not as important, file close, this was not in original program

// printf("\nExiting...\n"); removed, command functions shouldn't be chatty
return 0; // note the zero on return, means we executed properly
}


This program splits files up into 120 line chunks. It would actually be useful if to were to take the chunk size as well as the file name from the command line. Also, an output file prefix would also be good.

User is offlineProfile CardPM
+Quote Post

markorulz1
RE: C To C++
4 Oct, 2008 - 06:45 PM
Post #7

New D.I.C Head
*

Joined: 18 Sep, 2008
Posts: 4

QUOTE(gabehabe @ 22 Sep, 2008 - 04:59 AM) *

If it has to be pure C++ you'll have to use fstream for the files.

The program need, to go to the 120 line, and all txt from 0 to 120 line, cut and paste in another txt doc, and continue with other lines ....etc, so i still can convert it in C++ :S
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 04:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month