0

This may be a simple question, but I'm new to C, and yet couldn't find any answer. My program is simple, it takes 21 lines of string input in a for loop, and print them after that. The number could be less or greater.

int t = 21;
char *lines[t];
for (i = 0; i < t; i++) {
    lines[i] = malloc(100);
    scanf("%s", lines[i]);
}
for (int i = 0; i < t; i++) {
    printf("%s\n", lines[i]);
    free(lines[i]);
}
...

So when I copy & paste the inputs at a time, my program hangs, no error, no crash. It's fine if there's only 20 lines or below. And if I enter by hand line by line, it works normally regardless of number of inputs.

I'm using XCode 5 in Mac OS X 10.10, but I don't think this is the issue.

Update:

I tried to debug it when the program hangs, it stopped when i == 20 at the line below: 0x7fff9209430a: jae 0x7fff92094314 ; __read_nocancel + 20

The issue may be related to scanf, but it's so confused, why the number 20? May be I'm using it the wrong way, great thanks to any help.

Update:

I have tried to compile the program using the CLI gcc. It works just fine. So, it is the issue of XCode eventually. Somehow it prevents user from pasting multiple inputs.

Quyen Nguyen Tuan
  • 1,677
  • 17
  • 23

2 Answers2

2

Use fgets when you want to read a string in C , and see this documentation about that function:

[FGETS Function]

So you should use it like this :

fgets (lines[i],100,stdin);

So it'll get the string from the input of the user and you can have a look on these two posts as well about reading strings in C:

Post1

Post2

I hope that this'll help you with your problem.

Edit :

#include <stdio.h>
void main(){
int t = 21;
int i;
char *lines[t];
for (i = 0; i < t; i++) {
    lines[i] = malloc(100);
    fgets(lines[i],255,stdin);
}
for (i = 0; i < t; i++) {
    printf("String %d : %s\n",i, lines[i]);
    free(lines[i]);
}
}

This code gives :

enter image description here

As you can see , I got the 21 strings that I entered (From 0 to 20, that's why it stops when i==20).

I tried with your input ,here's the results :

enter image description here

Community
  • 1
  • 1
BahaEddine Ayadi
  • 987
  • 9
  • 20
  • Unfortunately, I tried to use fgets, but not the program still hangs. I'm trying `getchar()` now – Quyen Nguyen Tuan Aug 01 '15 at 14:53
  • See the editing that I did and tell me if the problem still coming off for you, just give me the input that you're passing to the program... @QuyenNguyenTuan – BahaEddine Ayadi Aug 01 '15 at 16:05
  • I tried you code, it works well with 21 lines, but increasing it produces the problem. This is my inputs (25 lines): https://dl.dropboxusercontent.com/u/5462074/input_tests.txt – Quyen Nguyen Tuan Aug 01 '15 at 17:33
  • I tried it with 25 lines, and it worked pretty well. I even tried it with 30 lines , and it's still going , If 100 lines is an easy job to do, i'll test it for you as well, but I think that it'll work as well. So here's what I think.. I think that it's a problem of the XCode program you're using, because, here's what I found [here](http://devcodehack.com/xcode-sucks-and-heres-why/) , they said that it crashes frequently. I'm using Code Blocks on windows, and it's the best for me.. Try to find some other program for you as well , and try the code and see what you'll got. @QuyenNguyenTuan – BahaEddine Ayadi Aug 01 '15 at 18:32
  • I attached the image when I enter your input as well, you can see it in my answer – BahaEddine Ayadi Aug 01 '15 at 18:35
  • Thanks @ayadi, maybe it's an issue of XCode. But I think it's due to some wrong configuration with gcc compiler of XCode, not of XCode ifself. XCode still is the best IDE for C programing on Mac. But maybe I'll try to compile & run C with command line, because XCode hides everything from me. – Quyen Nguyen Tuan Aug 02 '15 at 09:10
0

I wrote the same code and ran. It works. It might contain more than 99 characters (include line feed) per line... Or it might contain spaces and tabs.

scanf(3)

When one or more whitespace characters (space, horizontal tab \t, vertical tab \v, form feed \f, carriage return \r, newline or linefeed \n) occur in the format string, input data up to the first non-whitespace character is read, or until no more data remains. If no whitespace characters are found in the input data, the scanning is complete, and the function returns.

To avoid this, try

scanf ("%[^\n]%*c", lines[i]);

The whole code is:

#include <stdio.h>

int main() {

    const int T = 5;
    char lines[T][100];  // length: 99 (null terminated string)
    // if the length per line is fixed, you don't need to use malloc.
    printf("input -------\n");
    for (int i = 0; i < T; i++) {
        scanf ("%[^\n]%*c", lines[i]);
    }
    printf("result -------\n");
    for (int i = 0; i < T; i++) {
        printf("%s\n", lines[i]);
    }

    return 0;
}

If you still continue to face the problem, show us the input data and more details. Best regards.

fa11enprince
  • 122
  • 1
  • 6
  • I have tried your code, the problem is still there. Have you tried to copy & paste multiple inputs? For example, duplicate the input `jhrmxpdfhjjubrycaurjjwvkasfyxjkf` into 25 lines, paste them to the console and see what happen? – Quyen Nguyen Tuan Aug 01 '15 at 14:47