Thread: Reading an integer from file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Reading an integer from file

    I've tried various ways but I still can't seem to get it. In addition to reading from a file (the content is a matrix of integers), I'd have to enter it into a matrix consisting of characters in a separate file.

    Code:
    	/* reads and converts the numeric matrix into an alpha matrix */
    	for (i = 0; i < row; i++) //manipulates row
    	{
    	    for (j = 0; j < column; j++) //manipulates column
    	    {
    	        /* some code goes here */
                fputc()
    	    }
    	    fseek(f2, -1L, SEEK_CUR); //clears the last space of each row in f2
    	    fputc('\n', f2); //starts a new row in f2
    	}

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe you're hung up on this char data type and the int data type, and there is no problem handling them, unless you make one.

    Post up a sample of the input, and what you need to do with it, and we'll get it done.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Here's the full code. Sorry for the excessive comments, it's required for the assignment.

    Code:
    /*    
    This program generates an array of integers in the
    form of a 20x20 matrix. The matrix is then read and
    converted into its alphabetical equivalence.
    */
    
    #include <stdio.h> //standard input/output library
    
    int main (void) //main function
    {
    	/* declaration */
        FILE *f1, *f2; //declares pointers to file
        char input1[100], input2[100]; //declares file location variables
        int i, j, row, column; //declares counters and array size
        int matrix[100][100], m; //declares numeric matrix and integer
        char alpha[100][100], a; //declares alpha matrix and character
    
    	/* data input */
    	printf("Input the matrix file: ");
        gets(input1); //inputs location of numeric matrix
        printf("Input the alphabet file: ");
        gets(input2); //inputs location of alpha matrix
    	printf("Input the row number: ");
        scanf("%d", &row); //specifies row number
    	printf("Input the column number: ");
        scanf("%d", &column); //specifies column number
    	printf("\n"); //prints a newline
    
    	matrix[row][column]; //sets array to the specified size
    	alpha[row][column]; //sets array to the specified size
    
    	f1 = fopen(input1, "w"); //opens numeric array for writing
    	f2 = fopen(input2, "w"); //opens alpha array for writing
    
    	/* checks if the file(s) is accessible */
        if (f1 == NULL || f2 == NULL) //executes if a file is inaccessible
    	{
    		printf("File access error.\n"); //displays error message
    		return 1; //exits the program with error code
    	}
    
        srand(time(NULL)); //seeds random number with current time
    
    	/* generates the numeric matrix */
    	for (i = 0; i < row; i++) //manipulates row
    	{
    	    for (j = 0; j < column; j++) //manipulates column
    	    {
                matrix[i][j] = rand() % 10; //random number from 0 to 9
    			fputc(matrix[i][j], f1);
    			fputc(" ", f1);
    	    }
    	    fseek(f1, -1L, SEEK_CUR); //clears the last space of each row in f1
    	    fputc('\n', f1); //starts a new row in f1
    	}
    
        rewind(f1);
    
    	/* reads and converts the numeric matrix into an alpha matrix */
    	for (i = 0; i < row; i++) //manipulates row
    	{
    	    for (j = 0; j < column; j++) //manipulates column
    	    {
    	        a = fgetc(f1);
                fputc(a, f1);
    	    }
    	    fseek(f2, -1L, SEEK_CUR); //clears the last space of each row in f2
    	    fputc('\n', f2); //starts a new row in f2
    	}
    
    	/* displays numeric arrays in console */
    	for (i = 0; i < row; i++) //manipulates row
    	{
    	    for (j = 0; j < column; j++) //manipulates column
    	    {
                printf("%d ", matrix[i][j]); //prints numeric matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	printf("\nThe above matrix converts to:\n\n");
    
    	/* displays alpha array in console */
    	for (i = 0; i < row; i++) //manipulates row
    	{
    	    for (j = 0; j < column; j++) //manipulates column
    	    {
                printf("%c ", alpha[i][j]); //prints alpha matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	/* checks if the file(s) closes correctly */
        if (fclose(f1) != 0 || fclose(f2) != 0) //executes if the file does not close correctly
        printf("File close error.\n"); //displays error message
    
        return 0; //exits the program normally
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    matrix[row][column]; //sets array to the specified size
    alpha[row][column]; //sets array to the specified size
    No, that does not work that way.... You cannot change array size.
    You are just accessing matrix[row][column] and the value is discarded.
    Again your compiler will give you warning....

    Code:
    			
    fputc(matrix[i][j], f1);
    You want to write integer to text file. Then use fprintf...
    Code:
    			
    fputc(" ", f1);
    Read the manual of fputc. your code shouldn't even compile.
    Here's fputc prototype.
    Code:
           int fputc(int c, FILE *stream);
    Last edited by Bayint Naung; 11-27-2010 at 02:13 AM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Actually, the file did compile. Aside from warnings for the fputc function, there was no mention of the matrix.

    Should I use something like malloc instead?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is using a file required for the assignment?

    The reason I ask is, that 0 to 9 is well within the range of a char and you could print out the digits just by changing the format specifier in printf(). For instance:

    Code:
    #include <stdio.h>
    
    int main(void) {
      int digits[]={0,1,2,3,4,5,6,7,8,9};
      int i;
      for(i=0;i<11;i++) {
        printf("%d ", digits[i]);
        printf("%c   ", digits[i];
      }
      return 0;
    }
    So you see what char's are - you print raw ascii (or whatever your system is using for it's character set) values. When you print out a number, the value of the raw character set is jumped up to at least what zero is equal to - you'll never see a number 4 looking like a diamond or a 5 looking like a spade suit on a deck of cards.

    But if we want to prove a point just make your print line of code, look like this:
    printf("%c ", digits[i] + '0'); //plus zero

    Just remember that char data type has a very limited range (usually just 8 bytes), so 255 is usually as high as it can go.

    Which means when you have your array of digits[], and you want to change digits[i] to letters[i], all you need to do is just
    Code:
    letters[i] = digits[i] + '0'; //plus zero, not oh
    And if you don't have to use a file, then don't. There is no need for one, to make this change, unless your assignment specifies it.

    Your code header says the matrix array will be 20 x 20, but your matrix array is 100 x 100, and then you want to change it again? You're confusing me, you know that, right?

    Could you post the actual assignment? Your description leaves me with more questions that it answers.
    Last edited by Adak; 11-27-2010 at 03:03 AM.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Give me some time to read your post. It was supposed to be a 20x20 matrix, but I went ahead and tried to set the array size to be user-defined, so I arbitrarily went for 100. I also generated the matrix since I was too lazy to write one. The program easily works if the conversion is done within the generator loop, but I'm required to read the matrix from a file. Also, using fputs is optional, but maybe I should give it a try.

    Here's the assignment:
    Code:
    First, create a text file containing 20 rows of 20 integers separated by spaces. The integers should be 
    in the range 0-9 and separated by spaces. Write a C program that reads the contents into a 20x20 
    (two dimensional)     array of   int. The C program     then   should   use   this  array  to  initialize a 20x21 
    array  of  char  by associating  each  integer  from 0 to 9, with a character  from ‘A’ to ‘J’.  For example, 
    0 could be associated with ‘A’, while 9 with ‘J’ correspondingly. The 21st character on each line/row in 
    the  array of  char should  be the  null character  ‘\0’ (added by the C program), making  each line/row 
    of such an array as a string.  The C program displays the resulting array of  char (printing  row-by-row 
    the strings)  on screen, and also  write it in into a text file. 
    
    Hints/notes: 
    
    1.     An input text file can be: 
    
    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9 
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
    5  5  5  5  5  5  6  6  6  6  4  4  4  4  4  4  5  5  5  6 
    1  3  5  7  9  0  2  4  6  8  1  3  5  7  9  0  2 4  6  8 
    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9 
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
    5  5  5  5  5  5  6  6  6  6  4  4  4  4  4  4  5  5  5  6 
    1  3  5  7  9  0  2  4  6  8  1  3  5  7  9  0  2 4  6  8 
    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9 
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
    5  5  5  5  5  5  6  6  6  6  4  4  4  4  4  4  5  5 5  6 
    1  3  5  7  9  0  2  4  6  8  1  3  5  7  9  0  2 4  6  8 
    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9 
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
    5  5  5  5  5  5  6  6  6  6  4  4  4  4  4  4  5  5  5  6 
    1  3  5  7  9  0  2  4  6  8  1  3  5  7  9  0  2 4  6  8 
    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9 
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
    5  5  5  5  5  5  6  6  6  6  4  4  4  4  4  4  5  5  5  6 
    1  3  5  7  9  0  2  4  6  8  1  3  5  7  9  0  2 4  6  8 
    
    2.     A sample run of the program is illustrated below: 
    
    ABCDEFGHIJABCDEFGHIJ 
    AAAAAAAAAAAAAAAAAAAA 
    FFFFFFGGGGEEEEEEFFFG 
    BDFHJACEGIBDFHJACEGI 
    ABCDEFGHIJABCDEFGHIJ 
    AAAAAAAAAAAAAAAAAAAA 
    FFFFFFGGGGEEEEEEFFFG 
    BDFHJACEGIBDFHJACEGI 
    ABCDEFGHIJABCDEFGHIJ 
    AAAAAAAAAAAAAAAAAAAA 
    FFFFFFGGGGEEEEEEFFFG 
    BDFHJACEGIBDFHJACEGI 
    ABCDEFGHIJABCDEFGHIJ 
    AAAAAAAAAAAAAAAAAAAA 
    FFFFFFGGGGEEEEEEFFFG 
    BDFHJACEGIBDFHJACEGI 
    ABCDEFGHIJABCDEFGHIJ 
    AAAAAAAAAAAAAAAAAAAA 
    FFFFFFGGGGEEEEEEFFFG 
    BDFHJACEGIBDFHJACEGI
    Last edited by 843; 11-27-2010 at 03:18 AM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Here's the latest program:

    Code:
    #include <stdio.h> //standard input/output library
    
    int main (void) //main function
    {
    	/* declaration */
        FILE *f1, *f2; //declares pointers to file
        char input1[100], input2[100]; //declares file location variables
        int i, j; //declares counters and array size
        int matrix[20][20];//declares numeric matrix
        char alpha[20][21]; //declares alpha matrix
    
    	/* data input */
    	printf("Input the matrix file: ");
        gets(input1); //inputs location of numeric matrix
        printf("Input the alphabet file: ");
        gets(input2); //inputs location of alpha matrix
    
    	f1 = fopen(input1, "w"); //opens numeric array for writing
    	f2 = fopen(input2, "w"); //opens alpha array for writing
    
    	/* checks if the file(s) is accessible */
        if (f1 == NULL || f2 == NULL) //executes if a file is inaccessible
    	{
    		printf("File access error.\n"); //displays error message
    		return 1; //exits the program with error code
    	}
    
        srand(time(NULL)); //seeds random number with current time
    
    	/* generates the numeric matrix */
    	for (i = 0; i < 20; i++) //manipulates row
    	{
    	    for (j = 0; j < 20; j++) //manipulates column
    	    {
                matrix[i][j] = rand() % 10; //random number from 0 to 9
                fprintf(f1, "%d ", matrix[i][j]);
    	    }
    	    fseek(f1, -1L, SEEK_CUR); //clears the last space of each row in f1
    	    fputc('\n', f1); //starts a new row in f1
    	}
    
        rewind(f1);
    
    	/* reads and converts the numeric matrix into an alpha matrix */
    	for (i = 0; i < 20; i++) //manipulates row
    	{
    	    for (j = 0; j < 20; j++) //manipulates column
    	    {
    	        if (fgetc(f1) == ' ') //executes when a space is found
    	        fseek(f1, +1L, SEEK_CUR); //moves the position forward
    
    	        alpha[i][j] = fgetc(f1) + 65; //conversion
                fprintf(f2, "%c ", alpha[i][j]); //writes to alpha file
    	    }
    	    fseek(f2, -1L, SEEK_CUR); //clears the last space of each row in f2
    	    fputc('\n', f2); //starts a new row in f2
    	}
    
    	/* displays numeric arrays in console */
    	for (i = 0; i < 20; i++) //manipulates row
    	{
    	    for (j = 0; j < 20; j++) //manipulates column
    	    {
                printf("%d ", matrix[i][j]); //prints numeric matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	printf("\nThe above matrix converts to:\n\n");
    
    	/* displays alpha array in console */
    	for (i = 0; i < 20; i++) //manipulates row
    	{
    	    for (j = 0; j < 20; j++) //manipulates column
    	    {
                printf("%c ", alpha[i][j]); //prints alpha matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	/* checks if the file(s) closes correctly */
        if (fclose(f1) != 0 || fclose(f2) != 0) //executes if the file does not close correctly
        printf("File close error.\n"); //displays error message
    
        return 0; //exits the program normally
    }
    Here's the output:
    Code:
    Input the matrix file: a.txt
    Input the alphabet file: b.txt
    7 3 0 8 3 4 4 2 2 7 8 7 9 3 0 3 8 5 9 7
    7 2 3 4 7 7 1 2 4 2 2 6 6 6 9 5 5 9 2 0
    5 3 0 7 8 0 8 0 6 6 1 2 8 9 0 3 2 5 3 6
    4 0 1 9 9 9 2 3 5 3 4 0 4 4 2 7 8 9 1 9
    9 8 2 1 0 9 3 7 7 1 3 0 6 5 4 4 5 0 5 5
    6 0 2 2 7 3 2 9 8 0 1 2 6 1 0 9 2 7 0 4
    3 1 5 0 0 3 3 4 3 6 6 0 0 8 1 2 6 0 6 1
    9 8 5 5 1 0 2 8 9 3 4 7 5 1 5 0 2 6 3 5
    0 2 5 6 6 2 3 7 0 6 3 5 1 0 7 1 7 0 6 0
    0 3 1 2 0 8 0 9 9 7 2 1 9 2 8 1 1 5 8 9
    5 1 7 2 9 2 0 9 7 2 2 9 0 4 0 5 3 6 2 5
    6 1 6 0 7 3 5 0 6 6 6 1 0 4 9 8 9 3 3 6
    4 5 5 7 1 3 3 1 7 7 0 0 7 9 3 6 7 5 6 7
    8 5 5 7 2 2 2 9 8 2 8 4 9 7 7 4 8 4 6 8
    7 4 5 7 8 9 9 8 5 5 3 6 2 6 7 9 1 9 4 2
    4 9 7 9 1 0 3 4 5 0 6 1 6 4 5 0 0 7 6 9
    8 4 1 6 5 6 8 4 1 6 3 8 6 7 9 4 7 9 9 9
    0 0 4 0 1 1 6 8 3 2 0 1 1 7 2 1 0 4 0 8
    6 4 2 3 9 5 7 6 1 5 8 5 4 4 0 9 8 0 7 9
    2 3 9 6 1 2 6 5 0 1 1 3 2 1 8 7 5 3 1 6
    
    The above matrix converts to:
    
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    
    Process returned 0 (0x0)   execution time : 3.416 s
    Press any key to continue.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would minimize the file handling on the disk. It's slow, it's unnecessary, and it doesn't add to the clarity of what you're trying to do. When you have the data you need, already in your array, you wouldn't go back to the file on the disk, and mess with it (especially since it includes things like spaces to be dealt with). Just use the matrix, it has what you need, right there.

    I haven't checked this for accuracy, but this is the way I would suggest to do this:
    Code:
    #include <stdio.h> //standard input/output library
    #include <stdlib.h> //for srand()
    
    #define ROW 20  
    #define COL 20
    
    int main (void) //main function
    {
    	/* declaration */
        FILE *fp1, *fp2; //declares pointers to file
        char input1[100], input2[100]; //declares file location variables
        int i, j; //declares counters and array size
        int matrix[ROW][COL];//declares numeric matrix
        char alpha[ROW][COL]; //declares alpha matrix
    
    	/* data input */
    	printf("Input the matrix file: ");
        gets(input1); //inputs location of numeric matrix
        printf("Input the alphabet file: ");
        gets(input2); //inputs location of alpha matrix
    
    	fp1 = fopen(input1, "w"); //opens numeric array for writing
    	fp2 = fopen(input2, "w"); //opens alpha array for writing
    
    	/* checks if the file(s) is accessible */
        if (fp1 == NULL || fp2 == NULL) //executes if a file is inaccessible
    	{
    		printf("File access error.\n"); //displays error message
    		return 1; //exits the program with error code
    	}
    
        srand(time(NULL)); //seeds random number with current time
    
    	/* generates the numeric matrix */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                matrix[i][j] = rand() % 10; //random number from 0 to 9
                if(j<COL-1)
                  fprintf(fp1, "%d ", matrix[i][j]);
                else
                  fprintf(fp1, "%d\n", matrix[i][j]); 
    	    }
    	}
    
    
    /* When data has been entered into your array, you don't 
       want to then retrieve that data from a file - always
       use the array. When fresh milk is in the refrigerator,
       you don't drive 10 miles to town, to get a gallon of milk.
    */
    
    	/* reads and converts the numeric matrix into an alpha matrix */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
    	        alpha[i][j] = matrix[i][j] + 65; //conversion
                    if(j<COL-1)
                      fprintf(fp2, "%c ", alpha[i][j]); //writes to alpha file
                    else
                      fprintf(fp2, "%c\n", alpha[i][j]); 
    
    	    }
    	}
    
    	/* displays numeric arrays in console */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                printf("%d ", matrix[i][j]); //prints numeric matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	printf("\nThe above matrix converts to:\n\n");
    
    	/* displays alpha array in console */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                printf("%c ", alpha[i][j]); //prints alpha matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    	/* checks if the file(s) closes correctly */
        if (fclose(fp1) != 0 || fclose(fp2) != 0) //executes if the file does not close correctly
        printf("File close error.\n"); //displays error message
    
        return 0; //exits the program normally
    }
    The defines are a godsend when you need to change the size of your array - just change it in ONE place, for the ROW or the COL, and all the rest of your code is fine.

    File pointers are fp with me. They may be fp_in or fp_out, or fp1 or fp2, but they're always "fp".

    I added stdlib.h for srand(). You may need to also add time.h for time(), depending on your compiler.
    Last edited by Adak; 11-27-2010 at 05:04 AM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I will look into your post, but I just discovered a fatal mistake at fopen. By the way, I agree with what you said. In fact, that was what I did at first (post #7). However, the assignment requires that the matrix is read from the file, which is why I'm still scratching my head right now.

    Thanks for the define help! I never thought of it.
    Last edited by 843; 11-27-2010 at 05:23 AM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Really? I ran it, and the output looked good, although I didn't check every number/letter combo for accuracy - 0=A, 1=B, etc.

    OK, then don't rewind the file pointer. close it, and re-open it for reading this time. Or you could open the file for r+w mode, and then rewind it, but you don't want to rewind a file opened in w mode. The first time you try to access it, you will cause the entire previous data in the file, to be destroyed.
    Last edited by Adak; 11-27-2010 at 05:29 AM.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Well, I replaced the access mode "w" with "w+" because I would need to read the file later. Anyway, this is the output after replacing. You will notice that the characters are a mix of small and capital letters, with discernible patterns and weird letter frequency.

    Code:
    Input the matrix file: a.txt
    Input the alphabet file: b.txt
    
    7 1 8 7 5 8 3 9 3 5 0 1 1 4 1 5 9 0 8 9
    3 3 5 7 9 6 6 2 6 6 7 7 1 4 6 2 2 3 0 0
    5 8 5 8 0 6 0 8 6 8 0 3 5 9 7 8 3 8 7 4
    9 6 0 5 6 3 9 3 7 0 9 7 5 7 1 2 4 1 0 2
    9 9 0 5 4 4 7 5 0 9 5 1 3 1 2 2 7 8 1 6
    9 8 3 0 2 0 9 4 5 7 2 6 5 5 9 1 2 5 3 0
    1 9 2 9 3 9 1 9 6 2 5 3 0 8 7 1 0 5 6 7
    1 4 9 8 2 9 6 5 1 6 2 8 8 1 1 5 6 8 0 1
    0 3 3 4 8 0 8 1 8 9 1 7 7 7 2 5 3 5 1 5
    9 3 8 0 4 2 0 2 6 7 7 3 3 1 4 1 9 1 4 3
    8 3 3 5 3 0 8 4 8 5 7 9 8 0 3 0 0 7 4 0
    6 6 7 7 5 0 9 0 7 7 8 4 1 3 7 4 9 7 8 9
    3 2 4 5 3 4 6 9 2 4 5 7 4 4 2 4 2 9 3 3
    9 4 6 3 3 9 0 8 7 3 9 1 0 2 7 3 5 2 4 6
    8 7 7 4 0 3 3 8 0 9 9 4 9 1 1 4 9 1 7 3
    9 6 7 9 9 6 2 5 2 8 7 3 9 4 1 0 6 3 3 1
    5 4 6 4 3 0 5 1 3 6 4 6 9 8 1 9 8 4 0 3
    1 2 9 1 0 3 0 4 4 1 1 3 9 6 9 2 6 1 9 1
    6 5 8 6 7 8 4 2 5 4 6 0 4 0 3 5 3 1 2 8
    2 3 0 5 3 7 4 1 4 2 4 9 4 1 6 0 7 7 9 3
    
    The above matrix converts to:
    
    x a r a y a x a v a y a t a z a t a v a
    q a r a r a u a r a v a z a q a y a z K
    t a t a v a x a z a w a w a s a w a w a
    x a x a r a u a w a s a s a t a q a q K
    v a y a v a y a q a w a q a y a w a y a
    q a t a v a z a x a y a t a y a x a u K
    z a w a q a v a w a t a z a t a x a q a
    z a x a v a x a r a s a u a r a q a s K
    z a z a q a v a u a u a x a v a q a z a
    v a r a t a r a s a s a x a y a r a w K
    z a y a t a q a s a q a z a u a v a x a
    s a w a v a v a z a r a s a v a t a q K
    r a z a s a z a t a z a r a z a w a s a
    v a t a q a y a x a r a q a v a w a x K
    r a u a z a y a s a z a w a v a r a w a
    s a y a y a r a r a v a w a y a q a r K
    q a t a t a u a y a q a y a r a y a z a
    r a x a x a x a s a v a t a v a r a v K
    z a t a y a q a u a s a q a s a w a x a
    x a t a t a r a u a r a z a r a u a t K
    
    Process returned 0 (0x0)   execution time : 5.912 s
    Press any key to continue.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're reading in the spaces, and they're being converted to a's.

    Post up your code for reading the file.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Your output looks good, but that was without the need to read the matrix file, so the access type would not matter.

    Here's my program and output. Some of the weirder characters do not show up correctly. Also, I noticed there was a beep while running the program. What does that mean?

    Code:
    #include <stdio.h> //standard input/output library
    #include <stdlib.h> //required for srand()
    
    #define ROW 20
    #define COL 20
    
    int main (void) //main function
    {
    	/* declaration */
        FILE *f1, *f2; //declares pointers to file
        char input1[100], input2[100]; //declares file location variables
        char str[100]; //declares string variable
        int i, j, c; //declares integers
        int matrix[ROW][COL];//declares numeric matrix
        char alpha[ROW][COL]; //declares alpha matrix
    
    	/* data input */
    	printf("Input the matrix file: ");
        gets(input1); //inputs location of numeric matrix
        printf("Input the alphabet file: ");
        gets(input2); //inputs location of alpha matrix
        printf("\n");
    
    	f1 = fopen(input1, "w+"); //opens numeric array for writing
    	f2 = fopen(input2, "w+"); //opens alpha array for writing
    
    	/* checks if the file(s) is accessible */
        if (f1 == NULL || f2 == NULL) //executes if a file is inaccessible
    	{
    		printf("File access error.\n"); //displays error message
    		return 1; //exits the program with error code
    	}
    
        srand(time(NULL)); //seeds random number with current time
    
    	/* generates the numeric matrix */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                matrix[i][j] = rand() % 10; //random number from 0 to 9
                fprintf(f1, "%d ", matrix[i][j]);
    	    }
    	    fseek(f1, -1L, SEEK_CUR); //clears the last space of each row in f1
    	    fputc('\n', f1); //starts a new row in f1
    	}
    
        rewind(f1);
    
    	/* reads from file and converts the numeric matrix into an alpha matrix */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
    	        if ((c = fgetc(f1)) != ' ')
    	        {
    	            alpha[i][j] = c + '0'; //conversion
                    fprintf(f2, "%c ", alpha[i][j]); //writes to alpha file
    	        }
    	    }
    	    fseek(f2, -1L, SEEK_CUR); //clears the last space of each row in f2
    	    fputc('\n', f2); //starts a new row in f2
    	}
    
    	/* displays numeric arrays in console */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                printf("%d ", matrix[i][j]); //prints numeric matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	printf("\nThe above matrix converts to:\n\n");
    
    	/* displays alpha array in console */
    	for (i = 0; i < ROW; i++) //manipulates row
    	{
    	    for (j = 0; j < COL; j++) //manipulates column
    	    {
                printf("%c ", alpha[i][j]); //prints alpha matrix
    	    }
    	    printf("\n"); //prints newline
    	}
    
    	/* checks if the file(s) closes correctly */
        if (fclose(f1) != 0 || fclose(f2) != 0) //executes if the file does not close correctly
        printf("File close error.\n"); //displays error message
    
        return 0; //exits the program normally
    }
    Code:
    Input the matrix file: a.txt
    Input the alphabet file: b.txt
    
    7 3 7 0 3 4 4 3 4 8 2 8 0 6 5 8 9 0 0 1
    0 3 1 0 8 5 1 3 0 9 7 8 1 6 0 4 0 6 6 5
    9 9 7 6 4 2 4 8 6 6 1 7 3 3 2 0 9 8 8 9
    2 2 1 4 3 8 3 0 0 1 1 2 2 6 1 5 6 8 7 3
    1 9 4 7 9 3 1 8 8 6 4 1 2 3 9 1 9 7 5 8
    1 5 1 7 5 0 4 3 2 2 6 6 8 2 3 6 7 4 7 5
    6 6 9 3 0 1 3 6 9 2 4 3 4 7 0 7 6 1 2 9
    8 9 3 2 2 6 7 3 5 1 1 5 1 9 0 9 7 8 5 2
    9 3 6 5 1 5 7 3 5 1 8 5 5 7 5 0 0 7 5 1
    2 8 1 7 8 0 1 9 4 9 8 4 8 1 2 7 7 0 0 2
    4 8 0 8 2 9 5 2 4 6 7 0 0 2 9 3 2 6 2 3
    5 0 5 3 1 2 4 4 3 5 7 3 7 8 1 6 1 1 2 5
    5 3 1 9 6 1 7 1 3 5 0 4 4 0 0 0 3 1 6 2
    9 0 7 4 4 8 1 8 3 1 0 2 4 6 1 7 3 3 5 2
    4 4 8 2 8 3 7 8 8 2 1 0 8 7 8 6 7 5 0 3
    5 1 9 8 4 5 0 9 5 0 4 1 0 0 0 2 0 4 0 6
    6 7 0 2 7 7 1 5 6 7 1 1 5 7 0 0 3 0 5 2
    5 7 1 6 5 3 4 2 3 1 5 4 4 0 4 9 6 1 6 9
    1 9 4 5 6 8 5 3 1 7 2 5 0 4 1 2 5 2 6 2
    9 7 4 7 3 9 1 1 8 7 9 5 1 1 4 5 1 1 0 6
    
    The above matrix converts to:
    
    g  c  g  `  c  d  d  c  d  h 
    b  h  `  f  e  h  i  `  `  a :
    `  c  a  `  h  e  a  c  `  i 
    g  h  a  f  `  d  `  f  f  e :
    i  i  g  f  d  b  d  h  f  f 
    a  g  c  c  b  `  i  h  h  i :
    b  b  a  d  c  h  c  `  `  a 
    a  b  b  f  a  e  f  h  g  c :
     c  a  h  h  f  i
    d  a  b  c  i ! a # i % g ' e ) h :
    a - e / a 1 g 3 e 5 ` 7 d 9 c ; b = b ?
    f A f C h E b G c I f K g M d O g Q e :
    f U f W i Y c [ ` ] a _ c A f C i E b G
    d I c K d M g O ` Q g S f U a W b Y i :
    h } i  c   b   b   f   g   c   e   a
    a   e   a   i   `   i   g   h   e   b :
    i   c   f   e   a   e   g   c   e   a
    h   e   e   g   e   `   `   g   e   a :
    b   h   a   g   h   `   a   i   d   i
    h   d   h   a   b   g   g   `   `   b :
    
    Process returned 0 (0x0)   execution time : 3.479 s
    Press any key to continue.
    Here's the actual output printed in b.txt
    Code:
    t v t y u t t s q t
    s z t v y y q u z y K
    y s q t x y v r q y
    x z r x s y r s r q K
    u u x w y u t s t x
    s s w r z r r w z v K
    u w x q x w y w s x
    t y w x v y y r y s K
    q q y w s x v z z v
    u u q y s r w t y x K
    v t y z w s u z v q
    z u u q u u w z x q K
    t q q y x r z y y y
    u u y s w t w v w y K
    u s r q w q y r y s
    s w s w s z z q x z K
    z y u x z t x t t x
    x v u s v w t w t q K
    q r s y u q v t s s
    y u y t v r w q u s K
    Last edited by 843; 11-27-2010 at 05:42 AM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sounds like you tried to print an ascii char with a value of 7 (\a) "alert".

    I see two problems: main one is you're adding '0' (ascii 48) to the alpha, instead of 'A' (65).

    minor problem is you have no logic to handle newlines yet.
    Last edited by Adak; 11-27-2010 at 05:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM