0

I want to use output of a callback function on another system command. but not working on it, can anybody help?----

#include <stdio.h>
#include <stdlib.h> 
    
int fastLine(const char *filename);

int main() {
    char* ip_file = "f1";  
    fastLine(ip_file); // to print callback   
 
    // want to use it on system command,, but not working
    system("ping fastLine(ip_file)");
        
    return 0;
}

////// 1st line of file callback function ////////
int fastLine(const char *filename) {
    char line[1000];
    FILE *fptr;
    if ((fptr = fopen(filename, "r")) == NULL) {
        printf("Error! opening file");
        // Program exits if file pointer returns NULL.
        exit(1);
    }
    // reads text until newline is encountered
    fscanf(fptr, "%[^\n]", line);
    printf("%s\n", line);   
    fclose(fptr);
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    `fastLine` (is that supposed to be named `firstLine`?) is not being used as a [callback](https://en.wikipedia.org/wiki/Callback_(computer_programming)), and I don't see any reason why it should be. If your intent is to run the external `ping` command with the first line from a specified file, you'll need to make `fastLine` *return* a string. See: [Returning string from C function](https://stackoverflow.com/questions/25798977/returning-string-from-c-function) – jamesdlin Aug 06 '20 at 05:27
  • Thank you for reply. I'm new in c language. Can you edit the script? –  Aug 06 '20 at 05:33

1 Answers1

0

You need to merge the line read from the file with the ping command using string functions. I've used sprintf() below.

Instead of using a local line variable in fastLine(), you can make this a parameter so that the caller can provide the string to fill in, and then use that as the result.

#include <stdio.h>
#include <stdlib.h> 
    
int fastLine(const char *filename, char *line);

int main() {
    char* ip_file = "f1";  
    char hostname[64];
    fastLine(ip_file, hostname); // to print callback   
    char command[100];
    sprintf(command, "ping %s", hostname);
    system(command);
        
    return 0;
}

////// 1st line of file callback function ////////
int fastLine(const char *filename, char *line) {
    FILE *fptr;
    if ((fptr = fopen(filename, "r")) == NULL) {
        printf("Error! opening file");
        // Program exits if file pointer returns NULL.
        exit(1);
    }
    // reads text until newline is encountered
    fscanf(fptr, "%[^\n]", line);
    printf("%s\n", line);   
    fclose(fptr);
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ping.c:9:23: error: too many arguments to function call, expected single argument 'filename', have 2 arguments fastLine(ip_file, hostname); // to print callback ~~~~~~~~ ^~~~~~~~ ping.c:4:1: note: 'fastLine' declared here int fastLine(const char *filename); ^ ping.c:18:5: error: conflicting types for 'fastLine' int fastLine(const char *filename, char *line) { ^ ping.c:4:5: note: previous declaration is here int fastLine(const char *filename); –  Aug 06 '20 at 05:52
  • I forgot to update the function declaration to match. – Barmar Aug 06 '20 at 05:59