Thread: Why won't this image be read in from a string?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Why won't this image be read in from a string?

    what i'm trying to have happen is after i run the program and write in image.txt, i want to have the 4-optioned menu to pop up. however that is not happening. does anyone have any suggestions as to the error? thank you.


    Code:
    #include <stdio.h>
    
        char inversion(char image[][200], char chars[], int rows, int columns);
        char translation(char image[][200], int up, int right);
        char rotate(char image[][200], int rows, int columns);
        char shrink(char image[][200], int rows, int columns);
    
    
    int main() {
      int i = 0;
      int j = 0;
      int choice;
      int exit = 0;
      int rows;
      int columns;
      int junk;
      char image[80];
    
      char chars[12] = {'M','W','B','H','A','S','I','+',';',',','.'};
      FILE *inputr; //input file pointer
      FILE *outp; //output file pointer
    
            printf("Please enter file name: ");
    
      //ATTEMPT TO OPEN FILE!
      scanf("%s", image);
      if((inputr = fopen(image,"r")) == NULL) { //if file cannot open
        printf("Could not open image.txt\n");
        return 1;
      } //end if
    
      else{ //if file opens
    
        for(i = 0; i < rows; i++){
            fscanf(inputr, "%c", &junk);
        for(j = 0; j < columns; j++){
            fscanf(inputr, "%c", &junk);
    
        } //end inner for loop
    }     //end outer for loop
    
    
        fclose(inputr); //close file when done reading it
    		////fscanf to read in, also do print
        } //end else
    
    
      //ATTEMPT TO CREATE A NEW FILE!
      if ((outp = fopen("finalimage.txt","w")) == NULL) { //if file cannot be created
        printf("Could not open output.dat\n");
        return 1;
      } //end if
      else { //if file creation is successful
    
    
    
        for(i = 0; i < rows; i++){
            fprintf(outp, "%c");
        for(j = 0; j < columns; j++){
            fprintf(outp, "%c");
    
        } //end inner for
    } //end outer for
    
         fclose(outp); //close file when done writing it
        } //end else
    
    ///print array to a new file
    
    
    
        printf("Would you like to:\n\n");
            printf("1. Inversion\n");
            printf("2. Translation\n");
            printf("3. Rotate\n\n");
            printf("4. Shrink-by-2");
            scanf("%d",&choice);
    
        if(choice > 4 || choice < 1){  /*Stops any possible input that would not process in the program*/
                printf("Invalid Entry, try again");
                scanf("%d",&choice);
        } //end if
    
         return 0;
    
        } //end the main
    *in case this doesn't make much sense... after the menu pops up, i'm going to select one of the options in order to move the image (made up of text characters) a certain way.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, get a compiler which will warn you when you screw up printf/scanf formats.
    Eg.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:35: warning: format ‘%c’ expects type ‘char *’, but argument 3 has type ‘int *’
    bar.c:37: warning: format ‘%c’ expects type ‘char *’, but argument 3 has type ‘int *’
    bar.c:52: warning: too few arguments for format
    bar.c:54: warning: too few arguments for format
    bar.c:19: warning: unused variable ‘chars’
    bar.c:13: warning: unused variable ‘exit’
    bar.c:36: warning: ‘columns’ may be used uninitialized in this function
    bar.c:34: warning: ‘rows’ may be used uninitialized in this function
    Second, improve your indent style, say something like this
    Code:
    #include <stdio.h>
    
    char inversion(char image[][200], char chars[], int rows, int columns);
    char translation(char image[][200], int up, int right);
    char rotate(char image[][200], int rows, int columns);
    char shrink(char image[][200], int rows, int columns);
    
    int main()
    {
        int i = 0;
        int j = 0;
        int choice;
        int exit = 0;
        int rows;
        int columns;
        int junk;
        char image[80];
    
        char chars[12] =
            { 'M', 'W', 'B', 'H', 'A', 'S', 'I', '+', ';', ',', '.' };
        FILE *inputr;               //input file pointer
        FILE *outp;                 //output file pointer
    
        printf("Please enter file name: ");
    
        //ATTEMPT TO OPEN FILE!
        scanf("%s", image);
        if ((inputr = fopen(image, "r")) == NULL) { //if file cannot open
            printf("Could not open image.txt\n");
            return 1;
        }                           //end if
    
        else {                      //if file opens
            for (i = 0; i < rows; i++) {
                fscanf(inputr, "%c", &junk);
                for (j = 0; j < columns; j++) {
                    fscanf(inputr, "%c", &junk);
                }                   //end inner for loop
            }                       //end outer for loop
    
            fclose(inputr);         //close file when done reading it
            ////fscanf to read in, also do print
        }                           //end else
    
        //ATTEMPT TO CREATE A NEW FILE!
        if ((outp = fopen("finalimage.txt", "w")) == NULL) {  //if file cannot be created
            printf("Could not open output.dat\n");
            return 1;
        }                           //end if
        else {                      //if file creation is successful
            for (i = 0; i < rows; i++) {
                fprintf(outp, "%c");
                for (j = 0; j < columns; j++) {
                    fprintf(outp, "%c");
                }                   //end inner for
            }                       //end outer for
    
            fclose(outp);           //close file when done writing it
        }                           //end else
    
    ///print array to a new file
        printf("Would you like to:\n\n");
        printf("1. Inversion\n");
        printf("2. Translation\n");
        printf("3. Rotate\n\n");
        printf("4. Shrink-by-2");
        scanf("%d", &choice);
    
        if (choice > 4 || choice < 1) { /*Stops any possible input that would not process in the program */
            printf("Invalid Entry, try again");
            scanf("%d", &choice);
        }                           //end if
    
        return 0;
    }                               //end the main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: Beaten!

    Crank up the warnings on your compiler. It should complain about using uninitialized variables. Here's the list I got:
    Code:
    $ gcc -Wall menu.c
    menu.c: In function ‘main’:
    menu.c:34: warning: format ‘%c’ expects type ‘char *’, but argument 3 has type ‘int *’
    menu.c:36: warning: format ‘%c’ expects type ‘char *’, but argument 3 has type ‘int *’
    menu.c:52: warning: too few arguments for format
    menu.c:54: warning: too few arguments for format
    menu.c:19: warning: unused variable ‘chars’
    menu.c:13: warning: unused variable ‘exit’
    menu.c:35: warning: ‘columns’ may be used uninitialized in this function
    menu.c:33: warning: ‘rows’ may be used uninitialized in this function
    1. You need to initialize rows and cols before you use them in your loops.
    2. You call fprintf(outp, "%c"), but you never give it a character to print out, so you end up printing garbage from the stack.
    3. Fix your error messages to give correct file names if they don't open.


    I modified your first set of loops to print rows and columns before hand, and got this:
    $ ./a.out
    Please enter file name: image.txt
    rows: 134514672 columns: 134513776
    Considering your loops are nested, your program is doing 18,094,076,592,636,144 fscanf calls (or fprintfs if you get to the second loop) on my system, most of which are failing, but you never check the return value to see if you reached the end of file. It seems like nothing is happening, but it is...just not what you think. You should get to your menu eventually, but I wouldn't wait for 18 million billion fscanf calls. On a 1GHz proc, that would be on the order of 18 million seconds (probably lots more, but that gives you a rough idea). Initializing rows and columns to zero got me to your menu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  2. How to read out the RGB values of an image?
    By pjeremy in forum C Programming
    Replies: 5
    Last Post: 05-28-2006, 08:49 AM
  3. How to read digits from an image?
    By loobian in forum C# Programming
    Replies: 1
    Last Post: 07-15-2005, 11:09 AM
  4. Read Image to a Matrix
    By scrapedbr in forum C Programming
    Replies: 3
    Last Post: 05-02-2003, 03:53 PM
  5. How to read a image in C++?
    By DramaKing in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 12:34 AM