I use MinGW 2.0.0-2 in windows 2000.
What at this moment does the program is simply read a file, remove the comments of C++ type and write them in a temporary file, which will be readed again. (Right now I don't delete it, since I want check if this step is correct)
This is the file where all the code is:
With red and blue are two ways neither working. Red commented with /**/ for both in and out, and blue for a longer piece of code where I open close and reopen again and again.
The second way it opens the second file always, but at the end the file is empty, it hasn't write to it.

/****  TRASV.CPP ********/
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <dirent.h>
#include <sys/stat.h>
#include "trasv.h"

using namespace std;

void trasv(char* inName, char* outName, bool outSame = false){
fstream tempFile, inFile, outFile;
char tipName[L_tmpnam + 1];
char *tempName;
char buffer[255], *point;
char first;
static bool isFirst = false;
 
 

///*** IOS::IN | IOS::OUT
/*tmpnam(tipName);
tempName = &tipName[1];
if (tempName == NULL){
cerr << "Nombre del archivo temporal inválido" << endl;
return;
}
tempFile.open(tempName, ios::out | ios::in);
if (!outFile.is_open()){
cerr << "No se abre el archivo temporal" << endl;
//return;
}*/

/*while(inFile.eof() == false){
inFile.getline(buffer, 254);
first = buffer[0];
if (first == '/')
point = &buffer[2];
else
point = buffer;
if (tempFile.is_open())
tempFile << point << endl;
cout << point << endl;
}
inFile.close();
tempFile.seekg(0, ios::beg);
while (!tempFile.eof()){
tempFile.getline(buffer, 255);
cout << buffer << endl;
}
tempFile.close();*/
 
 

//****OPEN CLOSE VARIOUS TIMES
inFile.open(inName, ios::in | ios::binary);
if (!inFile){
cerr << inName << " no se abre" << endl;
return;
}
int size;
char* conts;
inFile.seekg(0, ios::end);
size = inFile.tellg();
inFile.seekg(0, ios::beg);
conts = new char[size];
inFile.read(conts, size);
inFile.close();
tmpnam(tipName);
tempName = &tipName[1];
tempFile.open(tempName, ios::out);
if (!tempFile.is_open()){
cerr << "No se abre el archivo temporal" << endl;
}
else{
tempFile.write(conts, size);
tempFile.close();
}
delete[] conts;
int how = 0;
tempFile.open(tempName, ios::in);
if (!tempFile.is_open()){
cerr << "No se reabre el archivo temporal" << endl;
}
else{
conts = new char[size];
for (int i = 0; i < size; i++){
conts[i] = '\0';
}
while (!tempFile.eof()){
tempFile.getline(buffer, 255);
if (buffer[0] == '/')
point = &buffer[2];
else
point = buffer;
if (how == 0)
strcpy(conts, point);
else
strcat(conts, point);
how++;
}
tempFile.close();
tempFile.open(tempName, ios::out);
if (!tempFile.is_open()){
cerr << "Ya casi estaba..." << endl;
}
tempFile << conts << endl; <- I TRIED BOTH THIS AND WRITE WITH IOS::BINARY
}
 

cout << outName << endl;
strcpy(buffer, "rm ");
strcat(buffer, tempName);
cout << buffer << endl;
//system(buffer);
system("PAUSE");
}
 

Here is the file which immediately calls this:

//***** THRUDIR.CPP
#include "trasv.h"
#include <dirent.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>

using namespace std;

void insetstats(char* carpeta, int argc, char **argv, const r_type flags, int *posis){
DIR *entryDir;
struct dirent* entryFiles;
struct dirent *dummy;
char filename[256], outfil[256];
bool reUse = false;

cout << "setstat called: " << carpeta << endl;
for (int i = 0; i < argc; i++){
cout << argv[i] << endl;
}
entryDir = opendir(carpeta);
while((entryFiles = readdir(entryDir)) != NULL){
cout << entryFiles->d_name << endl;
switch (flags){
   case I_DIR | O_DIR:
   {
   cout << "I_DIR y O_DIR" << endl;
   strcpy(outfil, argv[posis[1] + 1]);
   strcat(outfil, entryFiles->d_name);
   reUse = false;
   }
   break;
   case I_DIR | O_FILE:
   {
   cout << "I_DIR y O_FILE" << endl;
   if (argv[posis[3] + 1][1] != ':'){
   strcpy(outfil, argv[posis[0] + 1]);
   strcat(outfil, entryFiles->d_name);
   strcat(outfil, ".mod");
   }
   else
   strcpy(outfil, argv[posis[3] + 1]);
   reUse = true;
   }
   break;
   case I_DIR | O_FILE | O_DIR:
   {
   cout << "I_DIR y O_DIR y O_FILE" << endl;
   strcpy(outfil, argv[posis[1] + 1]);
   strcat(outfil, argv[posis[3] + 1]);
   reUse = true;
   }
   break;
   case I_DIR:
   {
   cout << "I_DIR" << endl;
   strcpy(outfil, argv[posis[0] + 1]);
   strcat(outfil, entryFiles->d_name);
   strcat(outfil, ".mod");
   reUse = false;
   }
   break;
   }
if (entryFiles->d_name[0] != '.'){
strcpy(filename, argv[posis[0] + 1]);
strcat(filename, entryFiles->d_name);
trasv(filename, outfil, reUse);
}
}
cout << (int)flags << endl << endl;
closedir(entryDir);
}

This is the main file which calls this:

//***** FORMER.CPP
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <dirent.h>
#include "trasv.h"

using namespace std;

int main(int argc, char **argv){
fstream tempFile, inFile, outFile;
char *tempName = "./traspaso.tmp", inName[255], outName[255];
char buffer[255], *point;
char first;
DIR* direcIn;
DIR* direcOut;
int _i = 0, _o = 0, _I = 0, _O = 0;
r_type flags = 0;
int *puestos = (int*)malloc(4 * sizeof(int));

if (argc < 2){
cout  << "El nombre del archivo de entrada: ";
cin >> inName;
cout << "El nombre del archivo de salida: ";
cin >> outName;
trasv(inName, outName, false);
}
else{
for (int i = 1; i < argc; i++){
if (strcmp(argv[i], "-I") == 0 || strcmp(argv[i], "--indir") == 0){
strcpy(inName, argv[i + 1]);
flags |= I_DIR;
_I = i;
}
if (strcmp(argv[i], "-O") == 0 || strcmp(argv[i], "--outdir") == 0){
strcpy(outName, argv[i + 1]);
flags |= O_DIR;
_O = i;
}
if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--infile") == 0){
if (flags & I_DIR)
strcat(inName, argv[i + 1]);
else
strcpy(inName, argv[i + 1]);
flags |= I_FILE;
_i = i;
}
if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--outfile") == 0){
if (flags & O_DIR)
strcpy(outName, argv[i + 1]);
else
strcpy(outName, argv[i + 1]);
flags |= O_FILE;
_o = i;
}
}
puestos[0] = _I;
puestos[1] = _O;
puestos[2] = _i;
puestos[3] = _o;
if ((flags & I_FILE) && (_i < _I || _i < _o || _i < _I)){
cout << "Uso: la opción -i o --infile debe ser la última antes de los archivos de entrada" << endl;
exit(1);
}
if ((flags & I_DIR) && !(flags & I_FILE)){
insetstats(inName, argc, argv, flags, puestos);
} //if
} //else argc

//system("PAUSE");
return 0;
}

And this is the header:

//***** TRASV.H
#ifndef TRASV_H
#define TRASV_H

typedef unsigned char r_type;
void trasv(char*, char*, bool);
void insetstats(char*, int, char**, const r_type, int *posis);
 
 

#define I_FILE 1
#define O_FILE 2
#define I_DIR 4
#define O_DIR 8
#define IO_NOT 0

#endif