MyUserID 0 #1 November 4, 2002 Ok, I have compiled my program, and it runs, but it isnt doing what it should be. Basicaly, I'm suppose to get a list of numbers from a file and dump them into an array and then calculate the contents and stuff. I can work with the arrays fine when they have the right data in them. I think my problem is taking the information from the file and putting it into the array. The file is basically just a list of numbers. This is what I am doing now: fscanf(inputx,"%d",inputarrayx); Do I need to put a "&" before the array?? Hopefully somebody knows C programming enough to help me out. -Chris ------------------------------------------------------ Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines. Quote Share this post Link to post Share on other sites
CanuckInUSA 0 #2 November 4, 2002 First off it's been years since I looked at a C program (I am a Java developer) and without seeing the rest of the code, it makes things even more obscure. But it looks like you are missing the '&'. The '&' this tells the compiler is that you are specifying the address of the 'inputarrayx' variable not the contents and it's the address which likely needs to be passed in. Try not to worry about the things you have no control over Quote Share this post Link to post Share on other sites
MyUserID 0 #3 November 4, 2002 I tried it again with the '&' and it made absolutely no difference in what the program does. Arrghh! Thanks for the help though. Hopefully somebody else can add some insight. Here is the source code if you want to look further at it. ------------------------------------------------------ Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines. project4a.c Quote Share this post Link to post Share on other sites
CanuckInUSA 0 #4 November 4, 2002 Instead of the '&' try the '*' character? Try not to worry about the things you have no control over Quote Share this post Link to post Share on other sites
MyUserID 0 #5 November 4, 2002 It didnt like that at all. It was worth a shot though. ------------------------------------------------------ Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines. Quote Share this post Link to post Share on other sites
indyz 1 #6 November 5, 2002 You input method is broken. fscanf only reads one line at a time, at least on my system. So what you need to do is loop and keep a count of what line you are on, then do your fscanf like: fscanf(inputy,"%d",&inputarrayy[lineNum]); /* hint: start at zero */ There are far more efficient ways to do this. Your method of input takes O(n^2) operations, while it can be easily done in O(n) time. Quote Share this post Link to post Share on other sites
gman 0 #7 November 5, 2002 Try this, I use a sequence of fgets() then sscanf(): #include #define MAX 32 main(){ FILE *file_ptr; int data[50],counter=0, max_value; char file_name[] = "data.txt"; char buffer[32]; if(file_ptr = fopen(file_name, "r")){ while(fgets(buffer, MAX, file_ptr)){ sscanf(buffer, "%i", &data[counter++]); } } else{ printf("file is not there\n"); } max_value = counter; printf("Contents of array is:\n"); for(counter = 0; counter < max_value; counter++){ printf("%d\n", data[counter]); } fclose(file_ptr); } The file data.txt is a single column of ints: 22 14 15 35 45 67 999 Compile & run it and see if this is what you want. -G.L.- Quote Share this post Link to post Share on other sites
carbon 0 #8 November 5, 2002 I don't write much C anymore but when I did, I didn't use sscanf much. Wouldn't something like this work? if(file_ptr = fopen(file_name, "r")){ while(fgets(buffer, MAX, file_ptr)){ data[counter++] = atoi(buffer); } } Now days I'd do it like this: (PERL) while(<>) { print; chomp; $arr[$i]=$_; $total=$total+$arr[$i++]; } print "Total=$total\n";-Carbon Quote Share this post Link to post Share on other sites
wlie 0 #9 November 5, 2002 See if this is what you are trying to do: #include #include #include void main(void) { int iDone = 0; int iCounter = 0; FILE *inputx; char inputarrayx[20]; char buf[5]; inputx = fopen("datax.txt", "r"); while(!iDone) { if(feof(inputx)) { iDone = 1; break; } else { memset(buf, '\0', sizeof(buf)); fgets(buf, 5, inputx); inputarrayx[iCounter] = atoi(buf); printf("%i\n", inputarrayx[iCounter]); iCounter++; } } fclose(inputx); }My other ride is the relative wind. Quote Share this post Link to post Share on other sites
MyUserID 0 #10 November 5, 2002 Wow!!! Thanx everybody :). Beers all around on me! ------------------------------------------------------ Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines. Quote Share this post Link to post Share on other sites