Created
June 17, 2014 23:12
-
-
Save ciarand/2ed11842dce41242fa52 to your computer and use it in GitHub Desktop.
trouble with pointers dot c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define LINE_LENGTH 1024 | |
int get_line_with_prefix_from_file(const char* prefix, FILE* file, char* result_buf) { | |
char line_buf[LINE_LENGTH]; | |
int matches = 0; | |
while (fgets(line_buf, LINE_LENGTH, file)) { | |
// segfaults here | |
matches = sscanf(line_buf, prefix, result_buf); | |
// if we actually found something | |
if (matches == 1) { | |
return 1; | |
} | |
} | |
return 0; | |
} | |
int main(int argc, const char* argv[]) { | |
FILE *cpuinfo; | |
char result_buf[LINE_LENGTH]; | |
// normally check we got a valid pointer, but not in GIST MODE | |
cpuinfo = fopen("/proc/cpuinfo", "r"); | |
int success = get_line_with_prefix_from_file("model name%[^:] %s", cpuinfo, result_buf); | |
if (success) { | |
printf("processor type: %s", result_buf); | |
} | |
fclose(cpuinfo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment