• C

example on reading a file like a string

I need an example of opening a file in C
and printing the contents of the file. I need to
be able to print certain sections of the file
LVL 2
pesstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnondCommented:
to dump the whole file do something like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  FILE *inFile;
  char ch;

  inFile=fopen("test.txt");
  if (inFile==NULL)
  {
      printf ("error opening file.\n");
      exit(0);
  }
  while ( (ch=fgetc(inFile)) != EOF)
  {
      fputc(stdout);
  }

  return (0);
}


if you need to ouput sections, you need some more complex mechanisms to identify the begining and ending of the sections.

Will be glad to furthur assist.
Arnon David.
0
pesstAuthor Commented:
That is exactly what I wanted, what i want to do is print out 8 characters at a time and ignore any spaces inbetween the characters. can you suggest a way to do that.  you can go ahead and answer the question and I will award you the points
0
arnondCommented:
to print out 8 char at a time, use an 8 char array (char array[9]) and in the while loop do:

while ( (ch=fgetc(inFile)) != EOF)
{
  for (i=0;i<=7;i++)
  {
     if (ch==' ')
       continue; /* ignore spaces */
     array[i]=ch;   /*this fills the array with 8 non-space chars*/
  }
  array[9]='\0'; /* terminating NULL */
  printf ("%s",array); /* print out the 8 chars array - add '\n' if you want a new line in every iteration.*/
}

you can add more chars to ignore in the if statement like this:
if ( (ch==' ') || (ch='1') || (ch=='Z')) ......   will ignore spaces, 1's and capital z's.

Arnon David.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pesstAuthor Commented:
david,

your code doesn't do exactly what it is supposed to.
say I input the string 1 0 1 0 0 1 0 0.
it outputs:
111111111111111100000000000000001111111111111111
and so on just printing each character 16 times.
can you see what the problem is?
0
pesstAuthor Commented:
I fixed it. never mind
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.