Read formatted numbers from a txt file

This is a discussion on Read formatted numbers from a txt file within the C Programming forums, part of the General Programming Boards category; hi all, i'm working on a c program that would sort an array of numbers, and then compare with another ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    Read formatted numbers from a txt file

    hi all, i'm working on a c program that would sort an array of numbers, and then compare with another text file produced by a sorting program to see if they match.

    Before I dealt with numbers that are in one column, so I just used the code below and it did the job of getting the numbers into an array.

    Code:
    while ( !feof(streamSorted ) )
    	{
    		fscanf_s( streamSorted, "%d", &sorted[sortedSize] );
    		sortedSize++;
    	}
    Now I have to deal with numbers that has formatting. There are now two columns of numbers instead of just one. First column is random numbers, unsorted, and * marks the end of each group. 2nd column is sorted, and # marks the end of the group. Either column one or two could be empty. For example,

    2
    3
    1
    5
    4*
    1
    9 2
    8 3
    10 4
    12 5#
    11
    6
    7* 6
    13 7
    15 8
    ...


    I will need to load all of the unsorted groups into an array, and the sorted groups into another one.

    Someone suggested to use fgets() but I don’t exactly see how it can do the job?

    Can someone please point it out? Thanks so much…

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > Explanations of... > Why it's bad to use feof() to control a loop
    You could do something like "%d%c" with sscanf following each call to fgets. Then if the character is not a newline, but instead a marker, you could take appropriate action.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. read numbers from a file
    By haroonie in forum C Programming
    Replies: 5
    Last Post: 03-27-2005, 10:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21