Skip to content

Instantly share code, notes, and snippets.

@jonepl
Last active October 26, 2016 22:20
Show Gist options
  • Save jonepl/2c795f1c99fd632fdb65f5909836ddd3 to your computer and use it in GitHub Desktop.
Save jonepl/2c795f1c99fd632fdb65f5909836ddd3 to your computer and use it in GitHub Desktop.
Easy concatenation and writing to a file in C
#include <lib.h> // provides _syscall and message
#include <unistd.h> // provides function prototype
#include <stdio.h> // printf
int main() {
int processID = 10;
long timestamp = 123456789;
char fromState[20] = "RDY";
char toState[20] = "RUN";
char buffer[100];
FILE *fp = fopen("proc-states", "w+");
if (fp == NULL) { printf("Error opening file!\n"); return 0; }
char buffer[100];
sprintf(buffer, "%d\t%ld\t%s\t%s\n", processID, timestamp, fromState, toState);
fprintf(fp, "%s\n", buffer);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment