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 re=
aded
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 /**/ f=
or
both in and out, and blue for a longer piece of code where I open close a=
nd
reopen again and again.
The second way it opens the second file always, but at the end the file i=
s
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 =3D false){
fstream tempFile, inFile, outFile;
char tipName[L_tmpnam + 1];
char *tempName;
char buffer[255], *point;
char first;
static bool isFirst =3D false;
///*** IOS::IN | IOS::OUT
/*tmpnam(tipName);
tempName =3D &tipName[1];
if (tempName =3D=3D NULL){
cerr << "Nombre del archivo temporal inv=E1lido" << 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() =3D=3D false){
inFile.getline(buffer, 254);
first =3D buffer[0];
if (first =3D=3D '/')
point =3D &buffer[2];
else
point =3D 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 =3D inFile.tellg();
inFile.seekg(0, ios::beg);
conts =3D new char[size];
inFile.read(conts, size);
inFile.close();
tmpnam(tipName);
tempName =3D &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 =3D 0;
tempFile.open(tempName, ios::in);
if (!tempFile.is_open()){
cerr << "No se reabre el archivo temporal" << endl;
}
else{
conts =3D new char[size];
for (int i =3D 0; i < size; i++){
conts[i] =3D '\0';
}
while (!tempFile.eof()){
tempFile.getline(buffer, 255);
if (buffer[0] =3D=3D '/')
point =3D &buffer[2];
else
point =3D buffer;
if (how =3D=3D 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::BINAR=
Y
}
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 =3D false;
cout << "setstat called: " << carpeta << endl;
for (int i =3D 0; i < argc; i++){
cout << argv[i] << endl;
}
entryDir =3D opendir(carpeta);
while((entryFiles =3D readdir(entryDir)) !=3D 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 =3D false;
}
break;
case I_DIR | O_FILE:
{
cout << "I_DIR y O_FILE" << endl;
if (argv[posis[3] + 1][1] !=3D ':'){
strcpy(outfil, argv[posis[0] + 1]);
strcat(outfil, entryFiles->d_name);
strcat(outfil, ".mod");
}
else
strcpy(outfil, argv[posis[3] + 1]);
reUse =3D 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 =3D true;
}
break;
case I_DIR:
{
cout << "I_DIR" << endl;
strcpy(outfil, argv[posis[0] + 1]);
strcat(outfil, entryFiles->d_name);
strcat(outfil, ".mod");
reUse =3D false;
}
break;
}
if (entryFiles->d_name[0] !=3D '.'){
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 =3D "./traspaso.tmp", inName[255], outName[255];
char buffer[255], *point;
char first;
DIR* direcIn;
DIR* direcOut;
int _i =3D 0, _o =3D 0, _I =3D 0, _O =3D 0;
r_type flags =3D 0;
int *puestos =3D (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 =3D 1; i < argc; i++){
if (strcmp(argv[i], "-I") =3D=3D 0 || strcmp(argv[i], "--indir") =3D=3D 0=
){
strcpy(inName, argv[i + 1]);
flags |=3D I_DIR;
_I =3D i;
}
if (strcmp(argv[i], "-O") =3D=3D 0 || strcmp(argv[i], "--outdir") =3D=3D =
0){
strcpy(outName, argv[i + 1]);
flags |=3D O_DIR;
_O =3D i;
}
if (strcmp(argv[i], "-i") =3D=3D 0 || strcmp(argv[i], "--infile") =3D=3D =
0){
if (flags & I_DIR)
strcat(inName, argv[i + 1]);
else
strcpy(inName, argv[i + 1]);
flags |=3D I_FILE;
_i =3D i;
}
if (strcmp(argv[i], "-o") =3D=3D 0 || strcmp(argv[i], "--outfile") =3D=3D=
0){
if (flags & O_DIR)
strcpy(outName, argv[i + 1]);
else
strcpy(outName, argv[i + 1]);
flags |=3D O_FILE;
_o =3D i;
}
}
puestos[0] =3D _I;
puestos[1] =3D _O;
puestos[2] =3D _i;
puestos[3] =3D _o;
if ((flags & I_FILE) && (_i < _I || _i < _o || _i < _I)){
cout << "Uso: la opci=F3n -i o --infile debe ser la =FAltima 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
|