C to C++

C to C++

Page 1 of 1

6 Replies - 888 Views - Last Post: 04 October 2008 - 07:45 PM Rate Topic: -----

#1 markorulz1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-September 08

C to C++

Posted 20 September 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

#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:

This post has been edited by Martyr2: 20 September 2008 - 02:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C to C++

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: C to C++

Posted 22 September 2008 - 12:24 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: C to C++

Posted 22 September 2008 - 04:11 AM

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:
sprintf(buffer, "%d", br);  
strcat(buffer,".txt");


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

Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: C to C++

Posted 22 September 2008 - 04:59 AM

If it has to be pure C++ you'll have to use fstream for the files.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: C to C++

Posted 22 September 2008 - 05:30 AM

View Postmarkorulz1, on 20 Sep, 2008 - 02:53 PM, said:

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

#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:


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.

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


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5849
  • View blog
  • Posts: 12,709
  • Joined: 16-October 07

Re: C to C++

Posted 22 September 2008 - 06:18 AM

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.

#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.
Was This Post Helpful? 0
  • +
  • -

#7 markorulz1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-September 08

Re: C to C++

Posted 04 October 2008 - 07:45 PM

View Postgabehabe, on 22 Sep, 2008 - 04:59 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1