3

I have a problem with reading empty string in C. I want to read string from the following -

  1. ass
  2. ball
  3. (empty)
  4. cat

but when I use gets() it does not treat (empty) as string[2]. It reads 'cat' as string[2]. So how can I solve this problem?

char str1[15002][12];
char str2[15002][12];
char s[25];
map<string,int> Map;

int main()
{
    int ncase, i, j, n1, n2, count, Case;

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d",&ncase);

    Case = 1;
    while(ncase > 0)
    {
        Map.clear();

        //this is the necessery part
        scanf("%d %d\n",&n1,&n2);
        count = 0;

        printf("n1=%d n2=%d\n",n1,n2);
        for(i = 0; i < n1; i++)
        {
          gets(str1[i]);
        }

        for(i = 0; i < n2; i++)
        {
            gets(str2[i]);
        }

        //end of reading input

        for(i = 0; i < n1; i++)
        {
            for(j = 0; j < n2; j++)
            {
                strcpy(s,str1[i]);
                strcat(s,str2[j]);

                if(Map[s] == 0){
                    count += 1;
                    Map[s] = 1;
                }
            }
        }

        printf("Case %d: %d\n", Case, count);
        Case++;
        ncase--;
    }
    return 0;
}

and input can look like

I have given the code here. The input may be like

line1>1
line2>3 3
line3>(empty line)
line4>a
line5>b
line6>c
line7>(empty)
line8>b

And I expect

str1[0]=(empty).
str1[1]=a;
str1[2]=b;

and

str2[0]=c;
str2[1]=(empty);
str2[2]=b;




OK, at last I found the problem. It is the line

printf("n1=%d n2=%d\n",n1,n2);

which creates problem in taking input by gets(). Instead of taking newline with the integer n1, n2, then I take newline as a ("%c",&ch) and then everything is okay.

Thanks to everyone who answered me.

5
  • 1
    check what is string[2]? Commented Apr 30, 2010 at 8:29
  • Is this a text file where the third line is empty? Commented Apr 30, 2010 at 8:30
  • yes,this is a text file. Commented Apr 30, 2010 at 8:32
  • I did not got the answer.can any one check whats wrong with my gets(). Commented Apr 30, 2010 at 9:22
  • OK, now you posted the code and the expected outcome. What is the actual outcome you get? Commented Apr 30, 2010 at 9:26

4 Answers 4

3

Chances are, the string contains \r\n\0 (or \n\r\0 - never remember which comes first). \r\n is newline on Windows and \0 is the terminating character of the string.

In general, if the first character of the string is \r or\n, you read an empty string. FWIW this should work on all platforms:

char* string;
// initialize string and read something into it
if (strlen(string) == 0 || string[0] == `\r` || string[0] == `\n`)
  // string is empty

Update: you mention that you use gets, and read from a file. However, for the latter you need fgets, so there is some confusion here. Note that fgets includes the trailing newline character in the string returned, while gets does not.

Update3: The way you read from the file is indeed fishy. You reopen the standard input to read from the file - why??? The standard practice is to fopen the file, then read from it with fscanf and fgets.

Update2: stupid us (and clever @Salil :-). You say

it read 'cat' as string[3]

Since C arrays are indexed from 0, string[3] contains the 4th line read! The third line is stored in string[2] - I bet that will contain the empty string you are looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

i could not understand.as far as i know gets take everything untill a newline character pressed.so why it does not take (emptyline+newline) as a string??
@russell, see my 2nd update, I think that should give you the answer.
peter,actually i wanted to tell that it contain 4th line in in string 3.so that is not problem.
2

Output of this code:

#include <cstdio>

int main ()
{
    int i = 0;

    char string [256];
    while (gets(string)) {
        ++i;
    }
    printf("%d\n", i);

    return 0;
}

For this input

a
b

d

Is

4

Which means, gets() reads all lines correctly, which in turn means your code must be screwed up. Post it here.

Comments

2

First and foremost, do not use gets!!!!! It is a buffer overflow vulnerability, since you cannot specify the size of the destination buffer, and so gets() can easily overrun your buffer. Instead, use fgets() or getchar().

Since you are using map<string,int>, it is clear that you are actually using C++ code. In that case, an even better approach is to use the C++ iostreams libraries for your input and output.

Now that I've done with my rant, the problem is this... gets -- which, again, you should never ever use -- according to the spec, will read up until a newline, and "any <newline> shall be discarded". The function fgets() will copy the newline into the destination buffer, giving you the desired behavior.

Comments

0

If there is no string, how do you expect to read it?

Please give us a piece of code :)

==Later edit ==

OK:

"gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with '\0'. "

So basically, if you have:

char x[3];

gets(x);

Then this function will fill in x[0] with '\0'

If you read the manpage you'll see that gets is not recommended. Use fgets instead

4 Comments

for(i=0;i<4;i++) { gets(str1[i]); } there is no way to read an empty string??i want to read the string based on line if there is blank in line three,then gets should read this,so that string number corresponds to line number.
@russell, please post the code in your original answer instead of hiding it in a comment. Including the definition of str1!
Paul R: How can I add comments to the original question? Sorry, I've joined stackoverflow yesterday
@Paul R, your downvote is unfair. He doesn't have the right yet to add comments to other's posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.