Hola,
miren, fijense que tengo un problema: Tengo que hacer un codigo, en donde la consola me pida el que ingrese un archivo, que sera "Datos.txt" y el tiene ciertas instrucciones como:
Ingresar,101,Rodrigo,75,80
Ingresar,201,dsds,70,85
Ingresar,301,PS4,100,800
Eliminar,101,Rodrigo,75,80
Modificar,201,dsds,70,85
Lo que me piden es que se generen 2 archivos:
1) El archivo Salida.txt que contenga las instrucciones del Datos.txt pero como que en pasado, asi como:
Se ingreso: 101,Rodrigo,75,80
Se ingreso 201,dsds,70,85
Se ingreso: 301,PS4,100,800
Se elimino: 101,Rodrigo,75,80
Se modifico: 201,dsds,70,85
2) Un archivo binario "Maestro.dat" en donde los datos que se ingresen (que tienen que ser 777) sean ordenados conforme a su codigo ascii y que si la instruccion dice ingresar que lo ingrese, si dice eliminar que lo elimine y si dice modificar, que lo modifique.
Ya tengo algo avanzado el codigo, pero me falta la parte del binario, no se si me ayudan, el codigo esta aca:
#include <stdio.h> #include <string.h> #include <conio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <cstring> using namespace std; struct Producto{ char cod[10]; char name[50]; char precio[10]; char cantidad[5]; }; struct Producto X; char Linea[200]; FILE *out, *inn, *bin; void generarArchivos(){ char ifilename[100]; char ofilename[] = "Salida.txt"; char bfilename[] = "Maestro.bin"; printf("Ingrese Archivo de Entrada: "); gets(ifilename); printf("\n"); inn = fopen(ifilename,"r"); /* Abrir archivo entrada */ strcpy(Linea," "); strcat(Linea,X.cod); strcat(Linea,","); strcat(Linea,X.name); strcat(Linea,","); strcat(Linea,X.cantidad); strcat(Linea,","); strcat(Linea,X.precio); strcat(Linea,","); fputs(Linea,inn); out = fopen(ofilename,"w"); while (fscanf(inn,"%s",Linea) != EOF) { fprintf(out,"%s\n",Linea); } fclose(out); bin = fopen("Maestro.bin","w"); FILE* f = fopen("Datos.txt", "r"); // Determine file size fseek(f, 0, SEEK_END); size_t size = ftell(f); int length; std::ifstream is ("Datos.txt", std::ifstream::binary); if (is) { // get length of file: is.seekg (0, is.end); length = is.tellg(); is.seekg (0, is.beg); // allocate memory: char * buffer = new char [length]; // read data as a block: is.read (buffer,length); is.close(); // print content: std::cout.write (buffer,length); delete[] buffer; } char* alocar = new char[length]; rewind(f); fread(alocar, sizeof(char), size, f); //-------------- string str(alocar); //---------- int from=0; int from1=0; int from2=0; int from3=0; int from4=0; int to=0; int to1=0; int to2=0; int to3=0; int to4=0; string codigo=""; string nombre=""; string cantidad=""; string precio=""; string nuevo="";//aqui va el nuevo string while(str.find("Ingresar") != string::npos){//encontrar ingresar from=str.find("Ingresar"); from1= str.find(",",from+8); to1= str.find(",",from1+1); codigo=str.substr(from1+1,to1-from1-1); from2= str.find(",",from1+1); to2= str.find(",",from2+1); nombre=str.substr(from2+1,to2-from2-1); from3= str.find(",",from2+1); to3= str.find(",",from3+1); precio=str.substr(from3+1,to3-from3-1); from4= str.find(",",from3+1); to4= str.find("\n",from3); cantidad=str.substr(from4+1,to4-from4-1); //------------Add text printf("%s",codigo.c_str()); nuevo.append(codigo); nuevo.append(","); nuevo.append(nombre); nuevo.append(","); nuevo.append(precio); nuevo.append(","); nuevo.append(cantidad); nuevo.append("\n"); str.replace(str.find("Ingresar"),8,"holamundo"); } while(str.find("Eliminar") != string::npos){//encontrar ingresar str.replace(str.find("Eliminar"), 8, "Se Elimino"); } while(str.find("Modificar") != string::npos){//encontrar ingresar str.replace(str.find("Modificar"), 9, "Se Modifico"); } //------------RECEONVERTIR A CHAR ARRAY int TempNumOne=nuevo.length(); char * deressed = new char[TempNumOne]; for (int a=0;a<=TempNumOne-1;a++) { deressed[a]=nuevo[a]; } //----------- fwrite(deressed,sizeof(char),TempNumOne,bin); fclose(bin); fclose(inn); } void leerDatos(){ inn = fopen("Datos.txt","r"); out = fopen("Salida.txt","r"); printf("\nEl contenido del archivo es: \n\n"); while (feof(out) == 0) { fgets(Linea,50,out); printf("%s",Linea); } fclose(inn); fclose(out); } int main(){ generarArchivos(); leerDatos(); return 0; }
Edito para ponerte las etiquetas de código.