小编xia*_*shi的帖子

混淆了Python中的readline()返回

我是python的初学者.但是,当我尝试使用readline()方法时,我遇到了一些问题.

f=raw_input("filename> ")
a=open(f)
print a.read()
print a.readline()
print a.readline()
print a.readline()
Run Code Online (Sandbox Code Playgroud)

和我的txt文件是

aaaaaaaaa
bbbbbbbbb
ccccccccc
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在Mac终端上运行它时,我得到了这个:

aaaaaaaaa
bbbbbbbbb
ccccccccc
Run Code Online (Sandbox Code Playgroud)

似乎readline()根本不起作用.但是当我禁用print a.read()时,readline()会恢复工作.

这让我很困惑.有什么解决方案可以同时使用read()和readline()吗?

python

1
推荐指数
1
解决办法
268
查看次数

遇到malloc遇到的问题(strlen(char*)+ 1)

在下面是一个小测试,我在使用C编程时混淆了.

int main()
{
    char buff[100] = {};
    char* pStr = NULL;

    printf("Input the string\n");
    if (!fgets(buff, sizeof(buff), stdin))
    {
        printf("ERROR INPUT\n");
        return 1;
    }
    printf("%zd\n", strlen(buff));
    pStr = (char*)malloc(strlen(buff)+1);
    strcpy_s(pStr, sizeof(buff), buff);
    printf("%s\n", strlen(pStr));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试图用来fgets捕获输入字符串并将其存储在指定的内存中malloc.但是,当我尝试使用时malloc(strlen(char*)+1),程序编译时没有错误但运行失败.一旦我切换到malloc(sizeof(buff)),它工作正常.我很困惑.从而寻求你的帮助.

c

0
推荐指数
1
解决办法
117
查看次数

标签 统计

c ×1

python ×1