There is a main () function in it, the function file (file_name) is called with the address on the file that needs to be read into the array and returned to the variable in main (I sit in ubuntu 13.10 writing in NetBeans 7.4)
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <unistd.h> #include <stdlib.h> #include <string.h> char *file(const char *config_file) { FILE *fp = fopen(config_file, "r"); char line[1024]; char *ip_address[1024] = {}; char ch = getc(fp); int index = 0; int i = 0, i2 = 0; while (ch != EOF) { if (ch != '\n') { line[index++] = ch; } else { line[index] = '\0'; index = 0; ip_address[i] = malloc(strlen(line) * sizeof (char)); strcpy(ip_address[i], line); i++; } ch = getc(fp); } fclose(fp); return ip_address; } int main(int argc, char *argv[]) { int opt = 0, i; char *in_fname = NULL; char count = 0; while ((opt = getopt(argc, argv, "f:")) != -1) { switch (opt) { case 'f': in_fname = optarg; char *buff = file(in_fname); for (count = 0; count < 12; count++) { printf("%s", buff[count]); } case '?': if (optopt == 'f') { printf("Вы не выбрали файл\n"); } break; } } return 0; }
I'm trying to run like this
./app -f 'text.txt'
The contents of the text.txt file are just strings in "\ n", but in main () I want to get them without "\ n"