Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Thanked 0 Times in 0 Posts

    Exclamation Printing Charts from a file

    Input will come from a datafile, data.txt. the datafile will look like this:


    The program must parse the file and display the output below based upon the data from the dataFile.

    1. 5| *****
    2. 9| *********
    3. 8| ********

    Now I have gotten as far as the code below and printed the first set of data from the file to test it but I have tried both for loops and do-while to get the output but I am simply at a loss.

    #include <stdio.h>
    int main (void)

    FILE *ptrToFile=NULL;

    char myData[100]={'\0'};

    if (ptrToFile==NULL) {
    printf("The file was not opened successfully\n");


    printf("The file was opened successfully\n");

    printf("The file contains: \n %s",myData);



  • #2
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Thanked 11 Times in 11 Posts
    For future posts, if you wouldn't mind, please put your code in code tags. For example:
    &#91;CODE&#93; Your Code Goes Between These Tags &#91;/CODE&#93;
    I would use fgets if I were you, but fread works just as well, it just works differently. The major difference between the two is what they return. Basically fgets returns one line from the file, where as fread returns the whole file. I would imagine fread would be better for handling a large file...

    So, if you are using fgets, you will have loop through the file returning one line at a time. If you are using fread, you will have to loop through the data returned by fread.

    Check out these two short articles, they will explain the difference and how to use them.

    If you still need help, just show us your progress.
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...
    Get Firefox!


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts