-2

I am sorry the question was not very clear and the code i have so far is shown below and i am just stuck on how i can use the while loop to print the contents of the file.

#include<stdio.h>


int main()
{
FILE *number;


/* Open the file 'numbers' for reading */
number = fopen("numbers.dat", "r");

if (number != NULL) {
/* A bit of space for a line of text */

char lineOfText[100];
while (fgetc(lineOfText, 100, number) != NULL) {
printf("%s\n", lineOfText);
}
   fclose(number);
}
 /* sorry, my question was not clear and to clarify i am trying to 
Print out the contents of the file with one entry per line, my .dat file includes 
1,2,3,4,5,6,7,8,9 and i am trying to print them out in this format
1
2
3
4 and so on...

*/
user2960049
  • 19
  • 1
  • 2

1 Answers1

2

To start you off...

FILE * f;
int c;  
f=fopen ("numbers.txt","r");    
while((c = fgetc(f)) != EOF) printf("%c", isdigit(c)? c : ' ');     
fclose (f);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • Not what the OP asked. Where is the special case '6' handled? (But I'll accept the answer "left as homework for the OP".) – Jongware Dec 10 '13 at 21:47
  • 2
    @Jongware its hard to tell from what he asked what he actually means, whether the file is 123456 and he wants 1 2 3 4 5 6 or the file is 1,2,3,4,5,6 and he wants 1 2 3 4 5 6, or whether he wants the 6 dropped off or not, however, the main question in the subject was how to read a character at a time, and he has an example of that, so he can work out how to make it do what he wants – Keith Nicholas Dec 10 '13 at 21:59
  • Yeah well, "Questions asking for code .." et cetera. This one ought to have been closed - @jimmcnamara got it right in the very first comment. – Jongware Dec 10 '13 at 22:04