C examples for String:String Console Input
Print the part of the file starting at that position and proceed to the next newline character
#include <stdio.h> #include <stdlib.h> #define SLEN 81/* w w w . jav a 2s . c o m*/ void get(char * string, int n); int main(void){ FILE *fp; char filename[SLEN]; long pos; int ch; printf("Enter a filename: "); get(filename, SLEN); if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Could not open file %s.\n", filename); exit(EXIT_FAILURE); } printf("Enter a position: "); while (scanf("%ld", &pos) == 1){ if (pos < 0) break; fseek(fp, pos, SEEK_SET); while ((ch = getc(fp)) != EOF && ch != '\n') putchar(ch); putchar('\n'); printf("Enter a positition: "); } fclose(fp); return 0; } // read from stdin and replace first newline with null character void get(char * string, int n){ fgets(string, n, stdin); while (*string != '\0') { if (*string == '\n'){ *string = '\0'; break; } string++; } }