Thread: Help with reading from a text file

  1. #16
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  2. #17
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    Well a first clear error is that you are printing column2[loop] with specifier %i which is used for ints, but you are reading the data in as %f (float).

    the first column are integers and the second column is floats. So:

    1 45.6
    2 23.4
    3 12.4

    I need to read the 1,2 and 3 into column 1 as an integer array and 45.6, 23.4 and 12.4 into column2 as a float array

  3. #18
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    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]);
    thanks ill put in that change now and see if it works

  4. #19
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  5. #20
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    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

  6. #21
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    Exactly, you have:

    fscanf(.... "%i %f"....)

    and fprintf (.... "%i %i" ....)

    notice the discrepancy?
    ah good point, thank you

  7. #22
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  8. #23
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    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

  9. #24
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  10. #25
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    Have you made the change to the printf specifier like indicated above? Post your latest code.
    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);
    

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  12. #27
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    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.

    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

  13. #28
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Quote Originally Posted by claudiu View Post
    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.
    ok ill take a look at selection sorts and see if I can make use of it in my program. My teacher said as long as we reference any programs we take we are welcome to utilise things from the net so...

  14. #29
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    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

  15. #30
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Reading from a text file
    By stevedawg85 in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2006, 09:27 PM
  3. reading from text file
    By requiem in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 12:42 AM
  4. Reading from a text file....
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 10-20-2001, 10:50 AM
  5. Reading text out of a file
    By Twiggy in forum C Programming
    Replies: 1
    Last Post: 10-17-2001, 11:17 AM