D
D
doublench212014-10-27 07:39:41
C++ / C#
doublench21, 2014-10-27 07:39:41

Reading a string of a certain length in a loop without the extra stuff?

No matter how I tried to read the string into an array, say char[32] in a loop and immediately display it, everything would be fine until the string exceeded the specified dimensions. And I cleaned the buffer and tried fgets - nothing. Without a cycle as it should, in a cycle - nonsense.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Andrey Akimov, 2014-10-27
@Ostan

The question is not entirely clear, and without the source code it is even more incomprehensible.
At the end of the string read character by character, after writing to the array, you need to add the end-of-line character '\0' . So, for example, you can write the 30th character, then force the 31st to put '\0' and then say that everything, there is no place.

J
jcmvbkbc, 2014-10-27
@jcmvbkbc

@Doublench21 you fundamentally don't read the previous answers to your questions?
Here I wrote to you that you can use fflush(stdin)

A
Armenian Radio, 2014-10-27
@gbg

Show source text, it's razu
In C++ there is cin for input, that's two.
Don't forget that for a string of 32 characters you need an array of 33 characters.

D
doublench21, 2014-10-27
@doublench21

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int fd[2], fork_status1, fork_status2;
    char buff[20], str_[32];


    if(pipe(fd) == -1) {
        fprintf(stderr, "pipe - error!\n");
        exit(-1);
    } else {
        fork_status1 = fork();
        if(fork_status1 == -1) {
            fprintf(stderr, "fork1 - error!\n");
            exit(-2);
        } else if(fork_status1 == 0) {
            fork_status2 = fork();

            if(fork_status2 == -1) {
                fprintf(stderr, "fork2 - error!\n");
                exit(-2);
            } else if(fork_status2 == 0) { // Child process(2)
                printf("Write smth(2)...\n");
                while(strcmp(str_, "exit")) {
                    fgets(str_, 30, stdin);
                    write(fd[1], str_, strlen(str_));
                }
                exit(0);
            } else { // Child process(1)
                printf("Write smth(1)...\n");
                while(strcmp(str_, "exit")) {
                    fgets(str_, 30, stdin);
                    printf("%s", str_);  //<-----------------------------------
                    write(fd[1], str_, strlen(str_));
                }
                exit(0);
            }

        } else { // Parent process
            while(strcmp(buff, "exit")) {
                read(fd[0], buff, strlen(str_));
                if(strcmp(buff, "exit")) {
                    //printf("%s\n", buff);
                }
            }
            close(fd[0]);
            close(fd[1]);
            exit(0);
        }
    }


    return 0;
}

I
Ivan Ershov, 2014-10-28
@iwanerhov

I didn’t understand much, but such things as std::stringno one canceled. With them, you will never go beyond the array boundary and collapse the heap :D

C
che2116, 2014-10-31
@che2116

The fgets function reads all characters from the stream, including the newline character, i.e. '\n'. Therefore, in order for the loop to end, you need to compare the entered string with "exit \ n", and not with "exit" as you have.

while(strcmp(str_, "exit\n"))
  {
    fgets(str_, 30, stdin);
    printf("%s\n", str_);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question