Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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:

    1:4
    2:9
    3:8

    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'};

    ptrToFile=fopen("myFile.txt","r");
    if (ptrToFile==NULL) {
    printf("The file was not opened successfully\n");

    }
    else
    {

    printf("The file was opened successfully\n");
    fread(myData,sizeof(myData),1,ptrToFile);

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

    }




    }

  • #2
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    For future posts, if you wouldn't mind, please put your code in code tags. For example:
    Code:
    &#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.
    http://www.cplusplus.com/reference/c.../cstdio/fread/
    http://www.cplusplus.com/reference/c.../cstdio/fgets/

    If you still need help, just show us your progress.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...


  •  

    Posting Permissions

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