Thread: Function to create array from file of digits returning count = 0

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Function to create array from file of digits returning count = 0

    I'm really new to C and I'm definitely open to guidance rather than the answer but i really just need to be pointed in the right direction more than anything so any help is appreciated. Also, I'm using functions supplied partially by my professor so if something is deprecated/not recommended that's why (so yes it is an assignment).


    My function is supposed to read from a file that formats numbers like this


    Code:
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14

    and so on... and return it into an array.


    Here is my array load:


    Code:
    int fillProcessesFromFile(struct processblock procs[], FILE* fPtr, int maxSize) {
        int cnt = 0;
        int pid;
        int uid;
        int aTime;
        int pri;
        int etr;
        int emn;
        int epc;
        // loop until end of file or array full - 
        while ((cnt < maxSize) && fscanf(fPtr, "%d %d %d %d %d %d %d", &pid, &uid, &aTime, &pri, &etr, &emn, &epc) != EOF) {
            procs[cnt].processId = pid;
            procs[cnt].userId = uid;
            procs[cnt].arrivalTime = aTime;
            procs[cnt].priority = pri;
            procs[cnt].expectedTimeRemaining = etr;
            procs[cnt].expectedMemoryNeed = emn;
            procs[cnt].expectedPctCPU = epc;
        }
        
        return cnt;
    }

    And here is where it's called:


    Code:
    int main() {
        struct processblock processes[600];
        FILE* filePtr;
        errno_t err;
        err = fopen_s(&filePtr, "processes.txt", "r"); // open file for reading
        int numProcs = 1;
        
        // make sure it opens
        if (err != 0) {
            perror("Error");
            printf("Can't open the file processes.txt\n");
            getchar();
            return 1; // indicate unsuccessful 
        }
        numProcs = fillProcessesFromFile(processes, filePtr, 600);
        fclose(filePtr);
    
    
        printf("%d", numProcs);
            getchar();

    and lastly here's my struct


    Code:
    struct processblock {
        int processId;
        int userId;         // pointer instead?
        int arrivalTime;   // time units in simulation, not actual time
        int priority;          // base priority
        int expectedTimeRemaining;
        int expectedMemoryNeed;
        int expectedPctCPU;     // to get an idea of whether CPU bound or IO bound
    };

    In main(), numProcs is returning 0 so it's obvious the count is returning 0. CAn't figure out why. THe file must be opening otherwise err would have errored.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, it would always return zero because you never change the "cnt" variable. You'd probably want to increment it at the end of each loop iteration.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Quote Originally Posted by GReaper View Post
    Yes, it would always return zero because you never change the "cnt" variable. You'd probably want to increment it at the end of each loop iteration.
    yikes.. careless mistake that was.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by briian View Post
    yikes.. careless mistake that was.
    Don't beat yourself up about it, we all make mistakes. Just remember that whenever something doesn't have the value you expect, you should look at the places where that value is set or changed and check for mistakes.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i count sum of digits in odd/even places?
    By cowa in forum C Programming
    Replies: 9
    Last Post: 11-16-2009, 11:43 PM
  2. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM
  3. count digits
    By Mariana in forum C++ Programming
    Replies: 7
    Last Post: 03-03-2004, 07:56 PM
  4. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM
  5. Replies: 3
    Last Post: 11-17-2002, 04:51 PM

Tags for this Thread