Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Characters not recognized as characters

Status
Not open for further replies.

DarkSpire

Programmer
Oct 5, 2006
5
0
0
US
I'm loading a 2D array with information from a file. The array loads the data properly; however, locations 9, 10, 12, 13, 17,18, and 19 are not recognized as characters, but the characters print correctly.

File content:
Code:
A 232 1150 1302 0 25 150 325 1 10 2006 10 20
B 232 1150 1302 0 25 150 325 1 10 2006 10 20
C 232 1150 1302 0 25 150 325 1 10 2006 10 20
D 232 1150 1302 0 25 150 325 1 10 2006 10 20
E 232 1150 1302 0 25 150 325 1 10 2006 10 20
F 232 1150 1302 0 25 150 325 1 10 2006 10 20
G 232 1150 1302 0 25 150 325 1 10 2006 10 20
H 232 1150 1302 0 25 150 325 1 10 2006 10 20
I 232 1150 1302 0 25 150 325 1 10 2006 10 20
J 232 1150 1302 0 25 150 325 1 10 2006 10 20
K 232 1150 1302 0 25 150 325 1 10 2006 10 20
L 232 1150 1302 0 25 150 325 1 10 2006 10 20
M 232 1150 1302 0 25 150 325 1 10 2006 10 20
N 232 1150 1302 0 25 150 325 1 10 2006 10 20
O 232 1150 1302 0 25 150 325 1 10 2006 10 20
P 232 1150 1302 0 25 150 325 1 10 2006 10 20
Q 232 1150 1302 0 25 150 325 1 10 2006 10 20
R 232 1150 1302 0 25 150 325 1 10 2006 10 20
S 232 1150 1302 0 25 150 325 1 10 2006 10 20
T 232 1150 1302 0 25 150 325 1 10 2006 10 20


Program:
Code:
#include <stdio.h>
#include <ctype.h>

int LoadFlightInfo(int flight[13][20]);

int main(void)
{

int flight[13][20];

LoadFlightInfo(flight);

return 0;

}


// LoadFlightInfo
// Inputs:	flight array
// Outputs:	flight array

int LoadFlightInfo(int flight[13][20])
{

int i, a;
char temp;

FILE *FlightInfo;

FlightInfo = fopen("lab1input.dat", "r");

for(i=0;i<20;i++)
{

	fscanf(FlightInfo, "%c%d%d%d%d%d%d%d%d%d%d%d%d", &flight[0][i], &flight[1][i], &flight[2][i], &flight[3][i], &flight[4][i], &flight[5][i], &flight[6][i], &flight[7][i], &flight[8][i], &flight[9][i], &flight[10][i], &flight[11][i], &flight[12][i]);
	fscanf(FlightInfo, "%c", &temp); // removes next line as character

a = isalpha(flight[0][i]);
printf("\ni = %d, %c, alpha = %d, temp = %c", i, flight[0][i], a, temp);
}

fclose(FlightInfo);

return flight[13][20];

} // End LoadFlightInfo Function

I'm very lost on why only a few characters are not recognized as characters. Actually they are not recognized as anything (digit, punctuation, etc.)

Any help is greatly appreciated!

Drew
 
Yet more scanf type woes...

> fscanf(FlightInfo, "%c", &temp);
If there is any stray character in your file, then this is just going to blow up.

> fscanf(FlightInfo, "%c%d%d%d%d%d%d%d%d%d%d%d%d", &flight[0]
%c expects a pointer to a char, not a pointer to an int.

Maybe something like
Code:
for(i=0;i<20;i++)
{
  char buff[BUFSIZ];
  char temp;
  int res;
  if ( fgets( buff, sizeof buff, FlightInfo ) != NULL ) {
    res = sscanf( buff, "%c%d%d%d%d%d%d%d%d%d%d%d%d", &temp, &flight[1][i], &flight[2][i], &flight[3][i], &flight[4][i], &flight[5][i], &flight[6][i], &flight[7][i], &flight[8][i], &flight[9][i], &flight[10][i], &flight[11][i], &flight[12][i]);  
    flight[0][i] = temp;
    if ( res != 13 ) {
      // error?
    }
  } else {
    // error?
  }
}
Using fgets() to read a whole line means you don't have to worry (too much) about whether the line is well-formed or not. Any minor trailing data just gets ignored.

> return flight[13][20];
This array element is outside the bounds of the array.
You've already updated the array, so it's best to make the function void, or return some kind of success/fail.
It is NOT returning the whole array.

--
 
That would be ideal; however, my assignment says I cannot use strings. That's why I'm having such an issue...
 
Might I recommend the use of the fscanf string " %c%d%d%d%d%d%d%d%d%d%d%d%d" instead of "%c%d%d%d%d%d%d%d%d%d%d%d%d"? The use of the preceding space before the %c field enables the whitespace skip for the character type; the end-of-line markers would normally show up as such. I would suspect that your input file might be a bit of a DOS/*nix mix, with some \n and some \r\n end-of-line markers, or even just some trailing spaces on your 8, 9, 11, 12, 16, 17, and 18 lines; your fscanf is currently unforgiving of whitespace variation between your final number scan and your initial character scan. A good fscanf string would really have all the whitespace delimiters shown for clarity, but they aren't functionally required for %d scans.
 
I'm not having a problem with white space. At least I don't I am. The first column is loaded into the array correctly; however, when I check to see if it is an alpha character the few list below are not recognized as characters or integers.
Code:
A 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
B 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
C 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
D 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
E 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
F 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
G 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
H 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
I 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
J 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
K 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
L 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
M 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
N 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
O 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
P 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
Q 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 256
R 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
S 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0
T 232 1150 1302 0 25 150 325 1 10 2006 10 20 alpha = 0

Above is the output of my array, flight[13][20]. Alpha is a TRUE or FALSE statement saying whether or not the first column has a character in it.

As you can see the zeros are not characters. I don't understand why it shows a character, but it isn't recogonized as one.

Does that help clarify a little?


I did try adding the space before %c%d%d, etc. and it was the same result.

Thanks for all the responses!!
 
This is great! Right after I posted, I tried one more thing. I initialized the array to 0 and everything is now recognized correctly!

I should have done this to begin with...

Thanks for all your help!

Drew
 
> I initialized the array to 0 and everything is now recognized correctly!
Which is your "sweep the problem under the carpet" solution to this...

> fscanf(FlightInfo, "%c%d%d%d%d%d%d%d%d%d%d%d%d", &flight[0]
%c expects a pointer to a char, not a pointer to an int.

If your int is uninitialised (0x12345678) and you scanf a single char into it (by abusing pointer types as you do), then you end up with say 0x123456??, where the ?? is the character you scanned.

Now the problem comes when you then try and use isascii() on it (which expects an int parameter), you end up doing isascii() on the whole int, not just the char you read in.

--
 
Also you point out that this is an "assignment".
Have you read the terms and conditions for using this forum?
School assignments are not allowed.


Trojan.
 
This was a small section of the assignment; however, I was not seeking help on the assignment. I was seeking help about arrays and using fscanf. The assignment was much more indepth.
 
So did you really fix the problem with your fscanf() call, or are you still working with the "initialise everything to zero to hide the real problem" way you chanced upon?



--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top