Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述具体来说,代码示例 here工作得很好,但仅当字符串存储在文件中时才有效. 有时我需要它来处理生成的字符串(存储在字符串变量中),但是我无法说服fgets的第三个参数来处理字符串变量,因为它是a pointer to a FILE structure. 或者也许有一个功能相当于可以在字符串上使用的fgets? 有什么建议?谢谢! 本着快速回答黑客的精神,这里是我刚写的“sgets”.它试图模拟fg 具体来说,代码示例 here工作得很好,但仅当字符串存储在文件中时才有效.

有时我需要它来处理生成的字符串(存储在字符串变量中),但是我无法说服fgets的第三个参数来处理字符串变量,因为它是a pointer to a FILE structure.

或者也许有一个功能相当于可以在字符串上使用的fgets?

有什么建议?谢谢!

解决方法 本着快速回答黑客的精神,这里是我刚写的“sgets”.它试图模拟fgets但使用字符串输入.

编辑修复了Monte指出的错误(谢谢).疯狂地输入一个实用程序,同时相信至少有15个具有完全相同想法的其他人疯狂地做同样的事情并不会导致经过良好测试的代码.不好意思原始版本在后续调用中包含换行符.

char *sgets( char * str,int num,char **input ){    char *next = *input;    int  numread = 0;    while ( numread + 1 < num && *next ) {        int isnewline = ( *next == '\n' );        *str++ = *next++;        numread++;        // newline terminates the line but is included        if ( isnewline )            break;    }    if ( numread == 0 )        return NulL;  // "eof"    // must have hit the null terminator or end of line    *str = '[+++]';  // null terminate this tring    // set up input for next call    *input = next;    return str;}int main( int argc,char* argv[] ){    // quick and dirty test    char *str = "abc\ndefghitjklksd\na\n12345\n12345\n123456\nabc\n\n";    char buf[5];    while ( sgets( buf,sizeof( buf ),&str ))        printf( "'%s'\n",buf );}
总结

以上是内存溢出为你收集整理的C的fgets是否可以使用字符串*而不是来自文件?全部内容,希望文章能够帮你解决C的fgets是否可以使用字符串*而不是来自文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
C的fgets是否可以使用字符串*而不是来自文件?_C_内存溢出

C的fgets是否可以使用字符串*而不是来自文件?

C的fgets是否可以使用字符串*而不是来自文件?,第1张

概述具体来说,代码示例 here工作得很好,但仅当字符串存储在文件中时才有效. 有时我需要它来处理生成的字符串(存储在字符串变量中),但是我无法说服fgets的第三个参数来处理字符串变量,因为它是a pointer to a FILE structure. 或者也许有一个功能相当于可以在字符串上使用的fgets? 有什么建议?谢谢! 本着快速回答黑客的精神,这里是我刚写的“sgets”.它试图模拟fg 具体来说,代码示例 here工作得很好,但仅当字符串存储在文件中时才有效.

有时我需要它来处理生成的字符串(存储在字符串变量中),但是我无法说服fgets的第三个参数来处理字符串变量,因为它是a pointer to a FILE structure.

或者也许有一个功能相当于可以在字符串上使用的fgets?

有什么建议?谢谢!

解决方法 本着快速回答黑客的精神,这里是我刚写的“sgets”.它试图模拟fgets但使用字符串输入.

编辑修复了Monte指出的错误(谢谢).疯狂地输入一个实用程序,同时相信至少有15个具有完全相同想法的其他人疯狂地做同样的事情并不会导致经过良好测试的代码.不好意思原始版本在后续调用中包含换行符.

char *sgets( char * str,int num,char **input ){    char *next = *input;    int  numread = 0;    while ( numread + 1 < num && *next ) {        int isnewline = ( *next == '\n' );        *str++ = *next++;        numread++;        // newline terminates the line but is included        if ( isnewline )            break;    }    if ( numread == 0 )        return NulL;  // "eof"    // must have hit the null terminator or end of line    *str = '';  // null terminate this tring    // set up input for next call    *input = next;    return str;}int main( int argc,char* argv[] ){    // quick and dirty test    char *str = "abc\ndefghitjklksd\na\n12345\n12345\n123456\nabc\n\n";    char buf[5];    while ( sgets( buf,sizeof( buf ),&str ))        printf( "'%s'\n",buf );}
总结

以上是内存溢出为你收集整理的C的fgets是否可以使用字符串*而不是来自文件?全部内容,希望文章能够帮你解决C的fgets是否可以使用字符串*而不是来自文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1255441.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存