从流中读取至多小于 size 的字符并将它们存储到 s 指向的缓冲区中。阅读在 EOF 或换行符后停止。如果读取换行符,则将其存储到缓冲区中。终止空字节('\0')存储在缓冲区中的最后一个字符之后
#include <stdio.h>
#include "get_string.h"
// print a line from stream using fgetc (only)
// reads in at most one less than size characters from stream and stores them into the
// buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is
// stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
void get_string(char *s, int size, FILE *stream) {
// PUT YOUR CODE HERE
}
#include <stdio.h>
#include "get_string.h"
// print a line from stream using fgetc (only)
// reads in at most one less than size characters from stream and stores them into the
// buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is
// stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
void get_string(char *s, int size, FILE *stream) {
int n=0;
char ch=fgetc(stream) ;
while( ch != '\n' && ch != EOF && n<size-1)
{
s[n++] = ch;
ch=fgetc(stream) ;
}
s[n] = '\0';
}
int main(int args,char *argv[])
{
if(args<2)
{
printf("error");
return 0;
}
char s[100] = {0};
FILE *fp = fopen(argv[1],"r");
if(fp == NULL)
{
printf("file open error");
return 0;
}
get_string(s,100,fp);
fclose(fp);
printf("%s",s);
return 0;
}