Thread: Need help with file in C programming

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    22

    Need help with file in C programming

    Hey guys, i'm studying files in C and when i do the following assignment, I get stuck.

    Problem: There are N boxes(1 ≤ N ≤ 1000). Each box has its own capacityCi (1 ≤ Ci ≤ 10000, 1 ≤ i ≤ N). Boxes are used to carry water from far away. Given the amount of water in each box. Calculate the minimum number of boxes needed to carry.

    Input: from file WATER.INP with structure:
    - the first line gives N (number of boxes)
    - the following N lines, each line has 2 parameters: Bi and Ci (0<= Bi <= Ci) which is th information of the i th box (Bi is amount of water, Ci is the capacity).There is at least one space between Bi and Ci.
    Output: create a file
    WATER.OUT that contains the minimum number of boxes needed.

    For examle:

    WATER.INP
    4
    0 1
    4 5
    0 2
    1 2

    WATER.OUT
    1


    I know how to print down a text file to screen, but not the files contains numbers. And I don't know how to access to each number in that file to use for my program.

    Hope you guys can help.
    Many thanks.

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    Here's an idea:
    1. Open the file with fopen()
    2. Read the first line with fgets() to obtain N - the number of additional rows to be expected.
    3. Iteratively call fgets() to obtain Bi and Ci (you can either loop while fgets() returns non-null value, or loop N times).

    The above assumes the input file is in the correct format, and also assumes that you only need help with reading the file, not with the algorithm itself.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    Can u give me an example of how you get the value N from the input file? I mean the full code of it.

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    The fscanf function will work better than fgets, if you can figure out how to set it up. Although I have not used fgets in 10 years, maybe it is fine. Actually the first line is N?

    Code:
    int N;
    fscanf(fptr, "%d", &N)
    Yeah, I wrote the code, it works easy with fscanf.
    Last edited by FourAngels; 09-18-2015 at 01:32 AM.

  5. #5
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Wow, this thread is now in the Projects and Recruitment forum...someone moved it here. In that case, I'll take the job!
    Code:
    #include<stdio.h>
    
    
    int main() {
        int N;
        float number;
        
        
        FILE * fptr;
        fptr = fopen("/fullpathname/Water.txt", "r");
        if (fptr == NULL) return 1;
        
        fscanf(fptr,"%d",&N);
        printf("The number of values is %d\n", N);
        
        while ( fscanf(fptr, "%f", &number) != EOF) 
            printf("Number: %f \n", number);
        
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Ugh, I moved the wrong thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about multiple file programming.
    By freiza in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2012, 01:06 PM
  2. File Handling in c programming........
    By cprogrammer101 in forum C Programming
    Replies: 6
    Last Post: 08-20-2007, 09:45 AM
  3. About C programming help on file handling
    By monkey12 in forum C Programming
    Replies: 3
    Last Post: 11-14-2006, 07:36 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM