...

View Full Version : Printing Charts from a file



cbertsch
11-03-2011, 02:48 PM
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);

}




}

bobleny
11-03-2011, 05:26 PM
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.
http://www.cplusplus.com/reference/clibrary/cstdio/fread/
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

If you still need help, just show us your progress.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum