Skip to content

Instantly share code, notes, and snippets.

@nrmmota
Created June 20, 2016 22:40
Show Gist options
  • Select an option

  • Save nrmmota/dd1c8c6f8aa59338ce0c12d20b370595 to your computer and use it in GitHub Desktop.

Select an option

Save nrmmota/dd1c8c6f8aa59338ce0c12d20b370595 to your computer and use it in GitHub Desktop.
File line parsing
#include <stdio.h> /* required for file operations */
#include <string.h>
#include <assert.h>
#include <stdlib.h>
FILE *fr; /* declare the file pointer */
main()
{
int n;
char line[80];
char *input, *input_port, *output, *output_port;
int in_port, out_port;
char *token, *string, *tofree, *in, *out;
fr = fopen ("multicast.conf", "rt"); /* open the file for reading */
/* multicast.conf example
* 239.192.1.1:1234\t239.192.2.1:1234
* 239.192.2.1:1234\t239.192.2.2:1234
*/
while(fgets(line, 80, fr) != NULL)
{
/* get a line, up to 80 chars from fr. done if NULL */
sscanf (line, "%ld", &elapsed_seconds);
/* convert the string to a long int */
//printf ("%s\n", line);
tofree = string = strdup(line);
in = strsep(&string, "\t");
input = strsep(&in, ":");
input_port = strsep(&in, ":");
in_port = atoi(input_port);
printf("input udp %s : %d\n",input, in_port);
out = strsep(&string, "\t");
output = strsep(&out, ":");
output_port = strsep(&out, ":");
out_port = atoi(output_port);
printf("output udp %s : %d\n",output, out_port);
free(in);
free(out);
free(tofree);
}
fclose(fr); /* close the file prior to exiting the routine */
} /*of main*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment