User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » quick bit of C programming help Page [1]  
Novicane
All American
15416 Posts
user info
edit post

I read in this txt file of 10 numbers. When I go to print the numbers I read in from the array, they don't print. Then when I refer to specific index's within the array nothing prints.

how can I store these numbers into an array?


f=fopen("numbers.txt","r");
if (!f)
return 1;
while (fgets(s,1000,f)!=NULL){
count++;
printf("%s",s);
num[count]=s;
}
fclose(f);


is what i got.

1/31/2008 9:30:59 AM

dakota_man
All American
26584 Posts
user info
edit post

how is num[] defined?

if it's just ints or something you can't just assign s to it

EDIT:

just use fscanf instead of fgets

http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html

[Edited on January 31, 2008 at 9:47 AM. Reason : .]

1/31/2008 9:40:52 AM

Novicane
All American
15416 Posts
user info
edit post

int num[10]; //im assuming this means an array of 10 integers.
int s; //integer

1/31/2008 9:56:03 AM

dakota_man
All American
26584 Posts
user info
edit post

well, I think fgets gives you characters, not ints. Also, printf("%s",s); is trying to print a string, not an int. If s was an int, you'd want to use %d instead.

1/31/2008 10:11:27 AM

Novicane
All American
15416 Posts
user info
edit post

ah! thats it.

1/31/2008 10:18:25 AM

 Message Boards » Tech Talk » quick bit of C programming help Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.