Thread: A Quick One

  1. #1
    Unregistered
    Guest

    A Quick One

    For the life of me I cant see why this doesnt work! Im trying to read in a 4 by 4 array of integers from a txt file. Any suggestions as to why its not working?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    FILE* pFileHandle;
    char filename[15];
    int array[4][4];
    int i;
    int j;
    
    printf("Please enter the name of the file you wish to use:\n");
    scanf("%s", filename);
    
    
    //error test
    	if((pFileHandle = fopen(filename, "r")) == NULL)
    	{
    		printf("Unable to open file : %s\n", filename);
    		return 0;
    	}
    	else
    
    //scan the contents of the input file to the array
        {
    		for(i=0; i<4; i++)
    		{
    			for(j=0; j<4; j++)
    			{
    				fscanf(pFileHandle, "%i\t", array[i][j]);
    			}
    		}
        }
    	fclose(pFileHandle);
    }

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    what is in the file? data type

  3. #3
    Unregistered
    Guest
    FILE* pFileHandle;

    try this
    FILE *pFileHandle;

  4. #4
    Unregistered
    Guest
    Nope that didnt work.

    The file i used is just a txt file. Its all integers and they're all tab spaced.

    1 2 3 4
    5 6 7 8
    9 8 7 6
    5 4 3 2

    this basically is the file but with tabs between the numbers and not spaces.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Can you try using %d with fscanf() ?

  6. #6
    Unregistered
    Guest
    its still giving an invalid page fault. im putting the exact path name in "C:\input.txt" and its giving the illegal operation box. If its an incorrect path name it returns the error. Thats working at least!

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Try putting the path name straight into the program, not using any variables. Remember to use \\ instead of \.

  8. #8
    Unregistered
    Guest
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    FILE* pFileHandle;
    char *filename="C:\\input.txt";
    int array[4][4];
    int i;
    int j;
    
    //printf("Please enter the name of the file you wish to use:\n");
    //scanf("%s", filename);
    
    
    //error test
    	if((pFileHandle = fopen(filename, "r")) == NULL)
    	{
    		printf("Unable to open file : %s\n", filename);
    		return 0;
    	}
    	else
    
    //scan the contents of the input file to the array
        {
    		for(i=0; i<4; i++)
    		{
    			for(j=0; j<4; j++)
    			{
    				fscanf(pFileHandle, "%d\t", array[i][j]);
    			}
    		}
        }
    	fclose(pFileHandle);
    }
    this is what ive changed it to. I hade to make "filename" into a pointer for it to compile but still its giving the illegal operation. Im not sure if changing it to a pointer was correct though.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh for heavens sake....

    fscanf(pFileHandle, "%d", &array[i][j]);

    You don't need the \t, scanf happly ignores leading white space (this includes tabs)

  10. #10
    Unregistered
    Guest
    Salem you're my hero. Thank you. Thanks for your help aswell Nutshell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM