Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: k-mm en 5 Julio 2017, 01:16 am



Título: Problemas con la creación de archivos en C
Publicado por: k-mm en 5 Julio 2017, 01:16 am
Hola buenas, resulta que soy nueva en esto de la programación y estoy haciendo un código en donde ingreso una imagen con formato bmp y debería crearse una imagen auxiliar (una copia de la imagen original). Funciona excelente en linux, pero no me funciona en Windows (lo probé en CodeBlocks y en  Dev-C++). ¿A qué se debe esto? AYUDA POR FAVOR !


Título: Re: Problemas con la creación de archivos en C
Publicado por: engel lex en 5 Julio 2017, 01:21 am
sin ver el código, puede ser cualquier cosa...


Título: Re: Problemas con la creación de archivos en C
Publicado por: k-mm en 5 Julio 2017, 01:47 am
 Perdón por lo desordenado, pero estoy esperando que funcione para hacerlo con funciones y dejarlo mas corto. En la opción 2 se supone que tendría que copiar la imagen. (Hasta el momento solo funciona con imágenes BMP de 24 bits).



Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct{
  6.    char marc1;
  7.    char marc2;
  8.    int tamArchivo;
  9.    short int reservado1;
  10.    short int reservado2;
  11.    int finalDatos;
  12. }FILEHEADER;
  13.  
  14. typedef struct{
  15.    unsigned int tamCabecera;
  16.    unsigned int ancho;
  17.    unsigned int alto;
  18.    short int numPlanos;
  19.    short int tamPunto;
  20.    unsigned int compresion;
  21.    unsigned int tamImagen;
  22.    unsigned int resoHoriz;
  23.    unsigned int resoVertic;
  24.    unsigned int tamTablaColor;
  25.    unsigned int contColorImport;
  26. }INFOHEADER;
  27.  
  28. typedef struct {
  29.    unsigned char azul;
  30.    unsigned char rojo;
  31.    unsigned char verde;
  32. }PIXEL;
  33.  
  34. int main ()
  35. {
  36.    int opcion;
  37.    printf ("\t\tESTEGANOGRAFIA\n\n");
  38.    printf ("Menu:  \n\n");
  39.    printf ("1. Calcular capacidad total de imagen.\n");
  40.    printf ("2. Esteganografiar.\n");
  41.    printf ("3. Decodificar.\n");
  42.    printf ("4. Salir.");
  43.    printf("\n\nIngrese una opcion: ");
  44.    scanf ("%d", &opcion);
  45.  
  46.    FILEHEADER fh;
  47.    INFOHEADER ih;
  48.    PIXEL pix;
  49.    FILE *fp;
  50.    FILE *fp2;
  51.    char archivo[30];
  52.    int i,j;
  53.  
  54.    if(opcion==1){
  55.        printf("\nIngrese nombre del archivo: ");
  56.        fflush(stdin);
  57.        scanf("%s", archivo);
  58.        fp = fopen(archivo,"rb");
  59.  
  60.        while(fp==NULL){
  61.            printf("\nEl archivo no ha sido encontrado.\n");
  62.            fflush(stdin);
  63.            printf("\nIngrese nombre del archivo: ");
  64.            scanf("%s", archivo);
  65.            fp = fopen(archivo,"rb");
  66.        }
  67.        fseek(fp,0,SEEK_SET);
  68.        unsigned char marc1 = fread(&fh.marc1, 1, sizeof(FILEHEADER), fp);
  69.        fseek(fp, 1, SEEK_SET);
  70.        unsigned char marc2 = fread(&fh.marc2, 1, sizeof(FILEHEADER), fp);
  71.        fseek(fp, 14, SEEK_SET);
  72.        unsigned int tamCabecera = fread(&ih.tamCabecera, 4, 1, fp);
  73.        fseek(fp, 18, SEEK_SET);
  74.        unsigned int ancho = fread(&ih.ancho, 4, 1, fp);
  75.        fseek(fp, 22, SEEK_SET);
  76.        unsigned int alto = fread(&ih.alto, 4, 1, fp);
  77.        fseek(fp, 28, SEEK_SET);
  78.        short int tamPunto = fread(&ih.tamPunto, 2, 1, fp);
  79.  
  80.        if(fh.marc1=='B' && fh.marc2=='M' && ih.tamCabecera==40){
  81.            if(ih.tamPunto==24){          //INICIO CALCULO CAPACIDAD
  82.                unsigned long long totBytes=(ih.ancho*ih.alto)*3;
  83.                printf("Capacidad total: %llu bytes\n", totBytes);
  84.            }
  85.            if(ih.tamPunto==16){
  86.                unsigned long long totBytes=(ih.ancho*ih.alto)*2;
  87.                printf("Capacidad total: %llu bytes\n", totBytes);
  88.            }
  89.            if(ih.tamPunto==32){
  90.                int totBytes=(ih.ancho*ih.alto);
  91.                printf("Capacidad total: %d bytes\n", totBytes)*4;
  92.            }   //FIN CALCULO CAPACIDAD
  93.        }
  94.        else{
  95.            while(fh.marc1!='B' || fh.marc2!='M' || ih.tamCabecera!=40){
  96.                printf ("\nImagen no valida.\n");
  97.                printf("\nIngrese nombre del archivo: ");
  98.                scanf("%s", archivo);
  99.                fp = fopen(archivo,"rb");
  100.  
  101.                while(fp==NULL){
  102.                    printf("\nEl archivo no ha sido encontrado.\n");
  103.                    fflush(stdin);
  104.                    printf("\nIngrese nombre del archivo: ");
  105.                    scanf("%s", archivo);
  106.                    fp = fopen(archivo,"rb");
  107.                }
  108.  
  109.                fseek(fp,0,SEEK_SET);
  110.                unsigned char marc1 = fread(&fh.marc1, 1, sizeof(FILEHEADER), fp);
  111.                fseek(fp, 1, SEEK_SET);
  112.                unsigned char marc2 = fread(&fh.marc2, 1, sizeof(FILEHEADER), fp);
  113.                fseek(fp, 14, SEEK_SET);
  114.                unsigned int tamCabecera = fread(&ih.tamCabecera, 4, 1, fp);
  115.                fseek(fp, 18, SEEK_SET);
  116.                unsigned int ancho = fread(&ih.ancho, 4, 1, fp);
  117.                fseek(fp, 22, SEEK_SET);
  118.                unsigned int alto = fread(&ih.alto, 4, 1, fp);
  119.                fseek(fp, 28, SEEK_SET);
  120.                short int tamPunto = fread(&ih.tamPunto, 2, 1, fp);
  121.            }
  122.  
  123.            if(ih.tamPunto==24){          //INICIO CALCULO CAPACIDAD
  124.                unsigned long long totBytes=(ih.ancho*ih.alto)*3;
  125.                printf("Capacidad total: %llu bytes\n", totBytes);
  126.            }
  127.            if(ih.tamPunto==16){
  128.                unsigned long long totBytes=(ih.ancho*ih.alto)*2;
  129.                printf("Capacidad total: %llu bytes\n", totBytes);
  130.            }
  131.            if(ih.tamPunto==32){
  132.                int totBytes=(ih.ancho*ih.alto)*4;
  133.                printf("Capacidad total: %d bytes\n", totBytes);
  134.            }   //FIN CALCULO CAPACIDAD
  135.        }
  136.  
  137.    }
  138.    if(opcion==2){
  139.        printf("\nIngrese nombre del archivo: ");
  140.        fflush(stdin);
  141.        scanf("%s", archivo);
  142.        fp = fopen(archivo, "rb");
  143.        fp2 = fopen("aux.bmp", "w+");
  144.  
  145.        while(fp==NULL){
  146.            printf("\nEl archivo no ha sido encontrado.\n");
  147.            fflush(stdin);
  148.            printf("\nIngrese nombre del archivo: ");
  149.            scanf("%s", archivo);
  150.            fp = fopen(archivo,"rb");
  151.            fp2 = fopen("aux.bmp", "w+");
  152.        }
  153.  
  154.        fseek(fp,0,SEEK_SET);
  155.        fread(&fh, sizeof(unsigned char), 14, fp);
  156.        fseek(fp,14,SEEK_SET);
  157.        fread(&ih,sizeof(unsigned char), 40,fp);
  158.        unsigned int valor[ih.ancho][ih.alto];
  159.        fseek(fp2, 0, SEEK_SET);
  160.        fwrite(&fh, sizeof(unsigned char), 14, fp2);    //TENER FWRITE AQUI HACE QUE CREE AUX AUNQUE SEA UN ARCHIVO INVALIDO O NO EXISTA
  161.        fseek(fp2, 14, SEEK_SET);
  162.        fwrite(&ih, sizeof(unsigned int), 40, fp2);
  163.  
  164.        if(fh.marc1=='B' && fh.marc2=='M' && ih.tamCabecera==40){
  165.            if(ih.tamPunto==24){
  166.                unsigned long long totBytes=(ih.ancho*ih.alto)*3;
  167.                for(i=0; i<ih.alto; i++){
  168.                    for(j=0; j<ih.ancho; j++){
  169.                        fseek(fp, (fh.finalDatos+3*(j+ih.ancho*i)), SEEK_SET);
  170.                        fread(&pix, sizeof(char), sizeof(PIXEL), fp);
  171.                        fseek(fp2, (fh.finalDatos+3*(j+ih.ancho*i)), SEEK_SET);
  172.                        fwrite(&pix, sizeof(char), sizeof(PIXEL), fp2);
  173.                    }
  174.                }
  175.  
  176.            }
  177.            if(ih.tamPunto==16){
  178.                unsigned long long totBytes=(ih.ancho*ih.alto)*2;
  179.            }
  180.            if(ih.tamPunto==32){
  181.                int totBytes=(ih.ancho*ih.alto)*4;
  182.            }   //FIN CALCULO CAPACIDAD
  183.        }
  184.        else{
  185.            while(fh.marc1!='B' || fh.marc2!='M' || ih.tamCabecera!=40){
  186.                printf ("\nImagen no valida.\n");
  187.                printf("\nIngrese nombre del archivo: ");
  188.                scanf("%s", archivo);
  189.                fp = fopen(archivo,"rb");
  190.                fp2 = fopen("aux.bmp", "w+");
  191.  
  192.                while(fp==NULL){
  193.                    printf("\nEl archivo no ha sido encontrado.\n");
  194.                    fflush(stdin);
  195.                    printf("\nIngrese nombre del archivo: ");
  196.                    scanf("%s", archivo);
  197.                    fp = fopen(archivo,"rb");
  198.                    fp2 = fopen("aux.bmp", "w+");
  199.                }
  200.  
  201.                fseek(fp,0,SEEK_SET);
  202.                fread(&fh, sizeof(unsigned char), 14, fp);
  203.                fseek(fp,14,SEEK_SET);
  204.                fread(&ih,sizeof(unsigned char), 40,fp);
  205.                unsigned int valor[ih.ancho][ih.alto];
  206.                fseek(fp2, 0, SEEK_SET);
  207.                fwrite(&fh, sizeof(unsigned char), 14, fp2);
  208.                fseek(fp2, 14, SEEK_SET);
  209.                fwrite(&ih, sizeof(unsigned int), 40, fp2);
  210.  
  211.                if(ih.tamPunto==24){
  212.                    unsigned long long totBytes=(ih.ancho*ih.alto)*3;
  213.                    for(i=0; i<ih.ancho; i++){
  214.                        for(j=0; j<ih.alto; j++){
  215.                            fseek(fp, (fh.finalDatos+3*(j+ih.ancho*i)), SEEK_SET);
  216.                            fread(&pix, sizeof(char), sizeof(PIXEL), fp);
  217.                            fseek(fp2, (fh.finalDatos+3*(j+ih.ancho*i)), SEEK_SET);
  218.                            fwrite(&pix, sizeof(char), sizeof(PIXEL), fp2);
  219.                        }
  220.                    }
  221.  
  222.                }
  223.                if(ih.tamPunto==16){
  224.                    unsigned long long totBytes=(ih.ancho*ih.alto)*2;
  225.                }
  226.                if(ih.tamPunto==32){
  227.                    int totBytes=(ih.ancho*ih.alto)*4;
  228.                }
  229.            }
  230.        }
  231.  
  232.  
  233.    }
  234. return 0;
  235. }


· Los códigos deben ir en etiquetas GeSHi
>aquí las reglas del foro (http://foro.elhacker.net/reglas.htm)
-Engel Lex