Thread: Easy Problem?: How to open/read a datafile

  1. #1
    Registered User
    Join Date
    Dec 2001

    Easy Problem?: How to open/read a datafile

    Hi, I have this program - im pretty sure its coded correctly, and i have a datafile - basically its just a few lines of numbers. Anyways im not sure about the procedure on how to open/read datafiles with the example below.

    Basically my program is about a mayors race with 4 candidates. I allready programed everything to count all the votes, percentage of votes, if a candidate has 50% of the vote he wins and it declares he/she the winner, if nobody gets 50% then it declares a runoff. Anyways im just having a problem opening and reading the datafile with all the vote figures. All the work i put into the code is useless if i cant read the datafile inputs!

    For example: datafile.txt (4 candidates = 4 columns of votes)
    192 48 206 37
    147 90 312 21
    186 12 121 38
    114 21 408 39
    267 13 382 29

    I use olympus to compile and run my program.
    gcc mayorrace.c (compiles the program - compiles fine)
    a.out -datafile.txt (runs the compiled program also points to the datafile - basically it just sits there and does nothing - i push escape and then it outputs alot of 0's instead of the actual datafile votes)

    Precinct        Candidate A     Candidate B     Candidate C     Candidate D
    1               0               0               0               134396          
    2               0               0               0               -12687724               
    3               0               0               0               0               
    4               0               0               -4260984                0               
    5               0               0               224592          0               
    Total           0               0               -4036392                -12553328               
    Percent         0               0               24              76
    What do i need to do? Thanks for your time Any tips would be greatly appreciated.

    The last program i had that used a datafile i used this line:
    FILE *fpr = fopen("datafile.txt", "r");
    but thats not working very well this time, either that or i dont know where to put it...
    Last edited by zipfur; 11-15-2002 at 02:04 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Text files are always opened the same. You either open for reading or writing. It's always done the same. It's up to you how you read that data however.

    Take a gander at fopen, fgets, fgetc, and fclose to get you started.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    a.out < datafile.txt makes the program work like a charm!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Zip, I'm in that class as well. I'm rather lost on how you got the array to read from a datafile...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with float and argv
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 03-04-2009, 02:47 PM
  2. Relatively easy math problem...
    By gcn_zelda in forum A Brief History of
    Replies: 6
    Last Post: 10-18-2005, 08:48 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Math Problem....
    By NANO in forum A Brief History of
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  5. Easy Peasy Function Problem
    By The Gweech in forum C++ Programming
    Replies: 10
    Last Post: 08-31-2001, 06:51 PM