Hi All,

I am tring to using the below function to read in a .txt file, which consist of a 10x10 matrix number.

I just cannot find out where goes wrong, it appears such a funny number -858993460...

void readinput(int in[][MAXCOLS])
	{
		int row ,col,ip;		//Declare row,col, ip as int type 
			
		inputFile=fopen("MatrixA.txt","r");		/*Open MatA.txt for reading*/
		fscanf(inputFile,"%d\n");		/*unuse integer*/
		fscanf(inputFile,"%d\n");		/*unuse integer*/

		for(row=0; row<MAXROWS; row++)	/* to keep on reading every number */
			{   	
				for(col=0; col<MAXCOLS; col++)			
				{
					fscanf(inputFile,"%d",&ip);	
					fscanf(inputFile," ");
					in[row][col]=ip;		
				}
			}
		return;			/*Empty return*/
		fclose(inputFile); /*Close the file stream*/
	}
fscanf(inputFile,"%d\n");

This is wrong. Whether you plan on using the value or not, it has to be stored in an object:

fscanf(inputFile, "%d", &ip);

However, you can tell fscanf() to throw it away with a modification to the specifier:

fscanf(inputFile, "%*d");

Finally, don't put '\n' in the fscanf() format string unless you really know what you're doing. *scanf() is not *printf(), despite the similarities. What works well with *printf() will totally mess you up with *scanf(), and literal whitespace is one of those things.

fscanf(inputFile," ");

This is redundant. The %d specifier will strip leading whitespace by default.

return;			/*Empty return*/
fclose(inputFile); /*Close the file stream*/

FYI, when you return from a function, every statement after the return does not execute. So your file isn't being closed.

it appears such a funny number -858993460...

That funny number is indicative of an uninitialized variable. In this case it means fscanf() is failing in some way.

Compare and contrast your code with mine:

void readinput(int in[][MAXCOLS])
{
    FILE *fp = fopen("MatrixA.txt", "r");
    
    if (fp == 0) {
        // Handle file open failure
        return;
    }
    
    // Skip N unused integers
    for (int i = 0; i < 2; i++)
        fscanf(fp, "%*d");

    // Populate the matrix
    for (int row = 0; row < MAXROWS; row++) {
        for (int col = 0; col < MAXCOLS; col++)
            fscanf(fp, "%d", &in[row][col]);
    }

    fclose(fp);
}

Hi, I've tried your way to modify my code, but it's still failing. Why is it so?

Or maybe can you suggest another way of reading some value from txt and save it to array?

This is wrong. Whether you plan on using the value or not, it has to be stored in an object:

fscanf(inputFile, "%d", &ip);

However, you can tell fscanf() to throw it away with a modification to the specifier:

fscanf(inputFile, "%*d");

Finally, don't put '\n' in the fscanf() format string unless you really know what you're doing. *scanf() is not *printf(), despite the similarities. What works well with *printf() will totally mess you up with *scanf(), and literal whitespace is one of those things.


This is redundant. The %d specifier will strip leading whitespace by default.


FYI, when you return from a function, every statement after the return does not execute. So your file isn't being closed.


That funny number is indicative of an uninitialized variable. In this case it means fscanf() is failing in some way.

Compare and contrast your code with mine:

void readinput(int in[][MAXCOLS])
{
    FILE *fp = fopen("MatrixA.txt", "r");
    
    if (fp == 0) {
        // Handle file open failure
        return;
    }
    
    // Skip N unused integers
    for (int i = 0; i < 2; i++)
        fscanf(fp, "%*d");

    // Populate the matrix
    for (int row = 0; row < MAXROWS; row++) {
        for (int col = 0; col < MAXCOLS; col++)
            fscanf(fp, "%d", &in[row][col]);
    }

    fclose(fp);
}

Just try modify line 6 by printf("error"); It may be possible that your code is unable to find the file
If it is so put the absolute path of your file like "c:\\test\\myfile.txt"

Hi, I've tried your way to modify my code, but it's still failing. Why is it so?

Well, since I tested the code before posting it, my guess is that you incorporated the changes into your code incorrectly. Assuming the file opens successfully, it's also possible that it doesn't contain valid integers in the expected order, but I can't confirm that because you didn't provide a sample.

Hi, so sorry about that, it's fixed now.
It just my fault with the logic in the main code. Thank you so much for you kindly help, now I know, scan is quite different from print...:)

Well, since I tested the code before posting it, my guess is that you incorporated the changes into your code incorrectly. Assuming the file opens successfully, it's also possible that it doesn't contain valid integers in the expected order, but I can't confirm that because you didn't provide a sample.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.