Hi,

Can someone please help me in this,,, i'm trying to write this program but i keep getting a segmentation fault . I think the problem is from the outerloop but i don't know how to fix it.

I have a file for example my file contains
hello
world
I
am
here
I'm trying to write a program that reads from the standard input until EOF and for each line read it will print out the file contents
for example:
if i type
fdsfhsfhsjfks
my program will display
hello
world
i
am
here
then the program will prompt me to enter another line and then display the file continents
it will continue to do this until i enter nothing and hit enter

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COM 100   
#define MAX_TEXT 80  
#define MAX_DATA 256  

void process_command(char command[],int command_no,char inp[],int input_count)
{
 printf("Input#%d %s\n Command #%d %s\n",input_count,inp,command_no,command);
}



int main(int argc, char *argv[])
{
  FILE *fp;                      /*file pointer*/
  char s[100];                 /*maximum length of the file*/
  char temp[MAX_COM];
  char temp2[MAX_COM];
  char line[40];
  char input[MAX_DATA];
  char* token;
  int count_command=1;
  int count_input=1;

  /*see if correct number of command line arguments*/
 if(argc!=2) {
      perror("Invalid command");
      exit(EXIT_FAILURE);
     }

    /*open file for input*/
    if((fp=fopen(argv[1],"r"))==NULL) {
     perror("Error Opening File");

 }

/*get the input from user and loop until end of file to process each command on the line of input*/

 while(getline(input,MAX_DATA)!=EOF)
{
   strcpy(temp2,input);
  while ((fgets(s,100,fp)!=NULL) && count_command<=MAX_COM+1)
        {

             if (strstr(s,"\n")!='\0');
                {
                          strcpy(temp,s);
                          count_command++;
                }
process_command(temp,count_command,input,count_input);
count_input++;
}


           }


     fclose(fp);

}

Where is function getline(). It isn't a standard C function so you must have written it yourself.

I thought you are supposed to write the new text entered from the keyboard into the file? Your program isn't doing that. And you need to rewind the file pointer back to the beginning of the file if you want to re-display the contents of the entire file.

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.