C - having trouble using stat() to get bytes -
char string[50], s[50]; struct stat buf; int counter = 0; while (fgets(string, sizeof string, stdin)) { if (strspn(string, "size ") == 5) { (int = 5; < strlen(string); i++) { s[counter] = string[i]; counter++; } s[counter] = '\0'; if (stat(s, &buf) < 0) return 1; //problem occured printf("file: %s\n", s); printf("size: %d\n", (int)buf.st_size); } } the context of program not important, trying use because after "size " name of file input. pass stat supposed give me bytes of given file if exists. ultimately, program returns 1 every time using stat wrong. please!
fgets() returns trailing newline character(s), 'filename' never correct.
replace:
for(int = 5; < strlen(string); i++) { s[counter] = string[i]; counter++; } s[counter] = '\0'; with:
char *source = &(string[5]), *dest = s; while((*source != 0) && (*source != 10) && (*source != 13)) { *dest++ = *source++; } *dest = 0; this way copying until 0 (end of string), or carriage return, or linefeed - rather appending cr and/or lf char string too.
Comments
Post a Comment