通过scanf函数输入字符串

通过scanf函数输入字符串

本文介绍了通过scanf函数输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问问有没有办法输入空白字符串通过scanf函数,
我使用此[^ \\ n]的输入什么那么不含换行符。是它正确?但是创建很多的问题,因为它似乎是存储在输入buffer.what是输入字符串。无论的最佳方式获取并与fgets正在创造很多的问题,

i wanted to ask is there way to input blank in string through scanf,i am using this [^\n] to input what so ever excluding newline .is it correct ?but is creating lot of problem as it seems to be stored in the input buffer.what is the best way to input string .both gets and fgets are creating lot of problems,

        while(strcmp(buf,"quit"))
		       {
			scanf("%*[^\n]",buf);
			n=send(connected,buf,strlen(buf),0);
			if(n<0)
			{
			perror("send");
			printf("error sending");
			exit(1);
			}
			//printf("server has send\n");
			n=recv(connected,buf,100,0);
			if(n<0)
			{
			perror("recv");
			printf("error recieving");
			exit(1);
			}
			//printf("waiting to recieve something\n");
			buf[n]='\0';
			printf("client:%s\n",buf);
		}

这是创造无限循环同样的事情一再重复。

this is creating infinite loop the same thing is repeated again and again.

推荐答案

如果您有麻烦都是空行,使用 STRCMP(\\ n,缓冲区)== 0

If all you are having trouble with is empty lines, use strcmp("\n", buffer) == 0.

您发布将不会工作很好,因为C将转化在%* [^ \\ n中的的'\\ n'字符的正则表达式]来字面换行符。有它更好地工作,你需要花葶斜线:%* [^ \\ \\ n]

The regexp you posted won't work very well because C will translate the '\n' char in "%*[^\n]" to a literal newline. To have it work better you need to scape the slash: "%*[^\\n]".

不过,似乎麻烦的是也与阅读,我建议你使用一个更好的函数,该函数。

However, it seems the trouble is also with the reading, i recommend you use a better function for that.

我用下面的code之前,从文件中读取任意大小的顺序线。

I have used the following code before to read sequential lines of arbitrary size from a file.

笔记情侣,但:


  1. 返回缓冲区需要为免费() D,你用它做后

  2. 的code废物几个字节的每一次迭代,但是这并不很明显,除非 BUFFER_SIZE 很小的比较到线的长度。

  1. The returned buffer needs to be free()d after you are done with it
  2. The code wastes a couple of bytes every iteration, but this is not really noticeable unless BUFFER_SIZE very small in comparision to the length of the lines.

在code,然而,保证一个完整的生产线将在 FILE * 读取,它会在'\\ n'结尾。

The code, however, guarantees that one full line will be read from the FILE * and it will end in '\n'.

/*
 * Initial size of the read buffer
 */
#define DEFAULT_BUFFER 1024

/*
 * Standard boolean type definition
 */
typedef enum{ false = 0, true = 1 }bool;

/*
 * Flags errors in pointer returning functions
 */
bool has_err = false;

/*
 * Reads the next line of text from file and returns it.
 * The line must be free()d afterwards.
 *
 * This function will segfault on binary data.
 */
char *readLine(FILE *file){
    char *buffer   = NULL;
    char *tmp_buf  = NULL;
    bool line_read = false;
    int  iteration = 0;
    int  offset    = 0;

    if(file == NULL){
        fprintf(stderr, "readLine: NULL file pointer passed!\n");
        has_err = true;

        return NULL;
    }

    while(!line_read){
        if((tmp_buf = malloc(DEFAULT_BUFFER)) == NULL){
            fprintf(stderr, "readLine: Unable to allocate temporary buffer!\n");
            if(buffer != NULL)
                free(buffer);
            has_err = true;

            return NULL;
        }

        if(fgets(tmp_buf, DEFAULT_BUFFER, file) == NULL){
            free(tmp_buf);

            break;
        }

        if(tmp_buf[strlen(tmp_buf) - 1] == '\n') /* we have an end of line */
            line_read = true;

        offset = DEFAULT_BUFFER * (iteration + 1);

        if((buffer = realloc(buffer, offset)) == NULL){
            fprintf(stderr, "readLine: Unable to reallocate buffer!\n");
            free(tmp_buf);
            has_err = true;

            return NULL;
        }

        offset = DEFAULT_BUFFER * iteration - iteration;

        if(memcpy(buffer + offset, tmp_buf, DEFAULT_BUFFER) == NULL){
            fprintf(stderr, "readLine: Cannot copy to buffer\n");
            free(tmp_buf);
            if(buffer != NULL)
                free(buffer);
            has_err = true;

            return NULL;
        }

        free(tmp_buf);
        iteration++;
    }

    return buffer;
}

这篇关于通过scanf函数输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:17