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!
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.
*edit: Please use code tags in the future, thanks!
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;
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
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.
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