hi guy i started learning c recently, and im trying to do exercises from the book the bad thing is they are not showing the solutions.
what i need to do is simply modify the program below so that instead of reading single characters at a time, it reads in blocks of text at a single go, and then displays the block on screen.
im not very sure how to make it work...:/

// read the file from comand lien and displys it
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(int argc, char *argv[])
{
  int fd ,numseats, i=0;
  
  fd = open(argv[1],O_RDWR);
  
  if(fd <0)
    {
      printf("file s% not found",argv[1]);
      exit(1);
    }

  while(read(fd, &numseats, sizeof(int)) > 0)
    printf("flight%4d: %4d seats available\n",
	   i++, numseats);
  
  /* finished */
  exit(0);
}

also it asks me to let the user choose where the output could be sent to whether screen or specified file.
i hope you could help me with this
thanks a lot guys in advance

hi guy i started learning c recently, and im trying to do exercises from the book the bad thing is they are not showing the solutions.
what i need to do is simply modify the program below so that instead of reading single characters at a time, it reads in blocks of text at a single go, and then displays the block on screen.
im not very sure how to make it work...:/

// read the file from comand lien and displys it
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(int argc, char *argv[])
{
  int fd ,numseats, i=0;
  
  fd = open(argv[1],O_RDWR);
  
  if(fd <0)
    {
      printf("file s% not found",argv[1]);
      exit(1);
    }

  while(read(fd, &numseats, sizeof(int)) > 0)
    printf("flight%4d: %4d seats available\n",
	   i++, numseats);
  
  /* finished */
  exit(0);
}

also it asks me to let the user choose where the output could be sent to whether screen or specified file.
i hope you could help me with this
thanks a lot guys in advance

You are reading blocks of text - the blocks just happen to be the sizeof int

read(fd, &numseats, sizeof(int))

For a choice of output use the write system call

ssize_t write(int fd, const void *buf, size_t count);

thank you very much your reply and for your solution i was trying to do it completely differently
here is the code

// read the file from comand line and displys it
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(int argc, char *argv[])
{
  int fd ,numseats, i=0, ans, bufs=50;
  FILE* lp;
  char  ile[bufs];
  fd = open(argv[1],O_RDWR);
  
  if(fd <0)
    {
      printf("file s% not found",argv[1]);
      exit(1);
    }
  printf("would you like the output be send to file??1/2  :");
  
 
  if(scanf("%d",&ans) == 1)
    {
      printf("please specifie the file\n");
      scanf("%s",ile);
      lp=fopen(ile, O_RDWR); 
  
      while(read(fd, &numseats, sizeof(int)) > 0)
	fprintf(lp,"flight%4d: %4d seats available\n", i++, numseats);
    }
  else
    {

  while(read(fd, &numseats, sizeof(int)) > 0)
    printf("flight%4d: %4d seats available\n", i++, numseats);
    }
  /* finished */
  exit(0);
}

this code does compile but it gives me segmentation fault when it comes to where i have to input the file name where i want the output to be do you know why this is happening
thanks again

thank you very much your reply and for your solution i was trying to do it completely differently
here is the code

// read the file from comand line and displys it
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main(int argc, char *argv[])
{
  int fd ,numseats, i=0, ans, bufs=50;
  FILE* lp;
  char  ile[bufs];
  fd = open(argv[1],O_RDWR);
  
  if(fd <0)
    {
      printf("file s% not found",argv[1]);
      exit(1);
    }
  printf("would you like the output be send to file??1/2  :");
  
 
  if(scanf("%d",&ans) == 1)
    {
      printf("please specifie the file\n");
      scanf("%s",ile);
      lp=fopen(ile, O_RDWR); 
  
      while(read(fd, &numseats, sizeof(int)) > 0)
	fprintf(lp,"flight%4d: %4d seats available\n", i++, numseats);
    }
  else
    {

  while(read(fd, &numseats, sizeof(int)) > 0)
    printf("flight%4d: %4d seats available\n", i++, numseats);
    }
  /* finished */
  exit(0);
}

this code does compile but it gives me segmentation fault

Doesn't your compiler produce any warnings? Which compiler are you using?

these are the warnings im getting but i was thinking they don't make much difference, the compiler im using is gcc

"readfile.c", line 12: warning: old-style declaration or incorrect type for: main
"readfile.c", line 22: warning: implicit function declaration: exit
"readfile.c", line 31: warning: improper pointer/integer combination: arg #2
"readfile.c", line 39: warning: implicit function declaration: read

Try this as a template for starting your code

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

#define BLK_SIZE 25

int main()
{
	char ch[BLK_SIZE + 1];
	int n = 0;

	int fd = open("filename", O_RDONLY);
	if (fd < 0)
	{
		fputs("could not open filename!\n", stderr);
		exit(EXIT_FAILURE);
	}

	while ((n = read(fd, ch, BLK_SIZE)) > 0)
	{
		ch[n] = '\0';
		/*if you want to see the read results then uncomment the next line*/
		/*fprintf(stdout, "\nread->%d\n", n);*/
		write(1, ch, n);
	}

	close(fd);
	exit(EXIT_SUCCESS);
}

Thanks a lot guys for your help i managed to sort it out and it does what i wanted it to do
Thanks for all your help

these are the warnings im getting but i was thinking they don't make much difference

With more GCC's switches turned on, you would have seen more warnings and actual errors. So, in addition to learning C, also get familiar with GCC, so that you can configure it to produce useful diagnostics. And in general, warnings shouldn't be ignored but fixed instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.