printf 限制为三个字符? [英] printf limited to three characters?

查看:66
本文介绍了printf 限制为三个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将这个功能简化为导致问题的原因,但我会尝试为这个问题提供足够的上下文,而不会让您仔细阅读一行又一行的文本:

I've stripped down this function to what's causing the problem, but I'll try to give enough context to this problem without making you peruse through lines and lines of text:

基本上,我将一个字符串输入到下面的函数中,如果字符串中的第一个字符是#,那么我想打印该字符串.就是这样.

Basically, I am feeding a string into the function below, and if the first character in the string is a # then I want to print the string. That's it.

但是,在执行此操作时,打印的字符串将被剪切为三个字符的长度.例如,如果输入字符串是# Hello, World!",只有# H"会被打印出来.

However, when doing this, the strings that are printed are cut at three characters in length. So for example, where the input string is "# Hello, World!" only "# H" will get printed.

我使用 fgets() 从以下输入文件中获取我的输入字符串:

I'm getting my input strings using fgets() from the following input file:

# 测试
#######
# 更多

# Testing
#######
# More

输出如下:

#T
###
###
#

# M

# T
###
###
#

# M

以下是相关的修剪函数:

Here are the relevant, trimmed functions:

int main(int argc,char *argv[]){

    FILE    *input;
    lineResponse response;

    //If a filename was specified, the file exists, and it is openable
    if(argv[1] != NULL && (input = fopen(argv[1],"r")) != NULL){
        char *currentLine = "";
        //Loop through the lines
        while(fgets(currentLine,sizeof(input),input)){
            response = parseLine(currentLine);
        }
    }

    fclose(input);

    getchar();

}

lineResponse parseLine(char *line){
    lineResponse response = {0,NULL}; //0 is the default lineType, denoting a line that cannot be parsed
    if(line != NULL){
        if(line[0] == '#'){
            printf("%s\n",line);
        }
    }
    return response;
}

lineResponse 返回与此问题无关.什么可能导致修剪?如果您需要更广泛的示例,我可以提供一个.

The lineResponse return is irrelevant to this problem. What could be causing the trimming? If you need a more extensive example, I can provide one.

推荐答案

char *currentLine = "";
 //Loop through the lines
 while(fgets(currentLine,sizeof(input),input)){

这是你的问题.您正在声明一个字符指针并为其分配一个字符串文字……然后尝试读入它.你似乎也不明白 fgets 的第二个参数;它读取的字符数少于一个,并以 \0 终止缓冲区.另请注意,换行符存储在缓冲区中,如果您不想要它们,则需要将其删除.

This is your problem. You are declaring a char pointer and assigning it a string literal ... then trying to read into it. You also seem to not understand the second argument of fgets; it reads one less than that number of characters and terminates the buffer with a \0. Also be aware that newlines are stored in the buffer and need to be removed if you don't want them.

这应该是:

char currentLine[1024]; /* arbitrary - needs to be big enough to hold your line */
while(fgets(currentLine,1024,input)) {

字符串文字(例如 char* = "This is a string literal")是不可变的(只读).它们是在编译时创建的.

String literals (e.g. char* = "This is a string literal") are immutable (read-only). They are created at compile time.

这篇关于printf 限制为三个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆