A second even clearer error which I have missed in the first post is that you forget the index to your arrays when reading in from the file.
fscanf(fp, "%i %f",&column1, &column2);
should be
fscanf(fp, "%i %f",&column1[loop], &column2[loop]);
A second even clearer error which I have missed in the first post is that you forget the index to your arrays when reading in from the file.
fscanf(fp, "%i %f",&column1, &column2);
should be
fscanf(fp, "%i %f",&column1[loop], &column2[loop]);
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Exactly, you have:
fscanf(.... "%i %f"....)
and fprintf (.... "%i %i" ....)
notice the discrepancy?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
ok its half working now
the column1 array is working and is displaying 1 2 3 4 5 etc
but the column2 array is only saving as 0 0 0 0 0 i.e. it doesnt seem to be picking up the correct values from the text file
Have you made the change to the printf specifier like indicated above? Post your latest code.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
excellent, its working now
thank you very much for your help
now I just have to do a bubble sort and mess around with the data lol
thank you so much for your help, patience and wonderful manners claudiu
Good stuff!
Unless bubble sort is required by the assignment specification I suggest using something like insertion sort. It is by far easier to implement and remember and is just as inefficient. Quality sort algorithms are a bit trickier and probably not needed for your assignment given the small sized data you are sorting.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
yes I made the changes, its all working now, here is the final code:
Code:FILE *fp; fp = fopen("C:\\TEMP\\LOOKUP.txt", "r"); // NOTE: Need to link this file to the Assignment 3 folder or copy n paste LOOKUP.txt to the temp folder in the C: drive for(loop=0; loop<16; loop++) { fscanf(fp, "%i %f",&column1[loop], &column2[loop]); printf("\n %i = %f", column1[loop], column2[loop]); } fclose(fp);
Sorry I meant selection sort not insertion sort. I always get those two mixed up in my head for some reason. I know the algorithms individually but I refer to them using the other one's name lol.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
well the next parts in my assignment basically ask me to randomly generate 10 numbers, then find out the equivilent value in the table for each randomly generated number, then i have to calculate the maximum and minimum values in each column and store the results in a text file, then I have to create functions for perform each of the bitwise operations on the 4 bits of the 10 generated numbers, and give the user the option to choose which function they'd like to do and thats part A done. After that, part B asks me to use arrays and pointers to: read the values from the results text file and then write them in reverse order in a new text file called results2, swap the order in which the columns are stored in the file results2.txt, sort and store the values of each column in ascending order.
So it doesnt specify a bubble sort, but thats what the teacher recommended but also said we're welcome to use a different method as long as the tasks at hand are completed.
I think getting the data from the text file to the program is just the begining to be honest
claudiu i was trying to leave a "like" or reputation point or something on your username but couldnt find out how to do it. where do i click? Im sure the forum has this because it has a "likes given/received" section
I think you have already done that and plenty of. I have 11 new notifications.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.