2

I'm reading a string from a file and then, trying to add zeros to the string till it reach 100, and then I'm printing the string, but I don't know what's wrong, I've tried more than one approach , but they all seems not to work.

int main(int argc, char *argv[])
{
    if (argc != 2 ){
        fprintf(stderr, "usage: server filename \n");
        exit(1);
    }
    FILE *file = fopen(argv[1], "r");
    if (file==0)
    {
        printf("file couldn't be opened\n");
        exit(1);
    } 
    int i;
    char str1[100];
    //char str2[100];
    //memset(str2,0,sizeof(str2));
     //for(i = 0; i < 100; i++)
         // fprintf(stdout, "str2[%u]: %u\n",i,str2[i]);

    while (fscanf(file, "%s", str1) != EOF) 
    {
        for(i=13 ; i < 100; i++)
            str1[i]=0;


    }

         for(i = 0; i < 100; i++)
         fprintf(stdout, "str1[%u]: %u\n",i,str1[i]);

    return 0;
}

but I print it I got

str1[0]: 119
str1[1]: 111
str1[2]: 114
str1[3]: 108
str1[4]: 100
str1[5]: 0
str1[6]: 0
str1[7]: 0
up till 99

I don't understand this although there is a string in the file "hello" and then I've tried this

int main(int argc, char *argv[])
{
    if (argc != 2 ){
        fprintf(stderr, "usage: server filename \n");
        exit(1);
    }
    FILE *file = fopen(argv[1], "r");
    if (file==0)
    {
        printf("file couldn't be opened\n");
        exit(1);
    } 
    int i;
    char str1[12];
    char str2[100];
    memset(str2,0,sizeof(str2));
     //for(i = 0; i < 100; i++)
         // fprintf(stdout, "str2[%u]: %u\n",i,str2[i]);

    while (fscanf(file, "%s", str1) != EOF) 
    {

     strcpy(str2,str1);

    }

         for(i = 0; i < 100; i++)
         fprintf(stdout, "str2[%u]: %u\n",i,str2[i]);

    return 0;
}

and I gave me the exact same result as the first one, so I don't understand what is happening here, and why I'm getting these results. I'll be grateful if you can explain what I'm doing wrong here. Thanks in advance.

7
  • What are you expecting to get? Commented Sep 27, 2012 at 14:16
  • I'm trying to get hello0000000000---till 99 but now it is either printing the zeros and the ascii or printing hello without zeros, is there no way to get them both?? Commented Sep 27, 2012 at 15:24
  • I think your main confusion is between the number zero and the character zero. Use '0' (in single quotes) if you want the character zero. Commented Sep 27, 2012 at 15:43
  • I tried to use it like this char str1[100] = {'0'}; wouldn't the zeros be of type char here, but still they are not being printed?? Commented Sep 27, 2012 at 15:49
  • If you use char str[100]={'0'} you are creating an array, where the first element is the character zero, and the rest of the elements are null. Commented Sep 27, 2012 at 15:52

5 Answers 5

2

In the last code snippet, you shouldn't do:

memset(str2,0,sizeof(str2));

but instead:

memset(str2,0,sizeof(char) * 100);

Check this link on sizeof: http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays#sizeof

3
  • 2
    I really don't believe in sizeof(100).
    – unwind
    Commented Sep 27, 2012 at 14:16
  • The size of one hundred is four :)
    – Lundin
    Commented Sep 27, 2012 at 14:19
  • If you want to simply print the string (without the trailing zeroes), do fprintf(stdout, "str2: %s\n", str2);
    – fableal
    Commented Sep 27, 2012 at 16:11
1

A lot of wrong things here.

Most obvious one to me is that you're printing each character as %u, which is unsigned int. You should print each character as char, that is, %c.

Second, as pointed out by fableal, your memset() is also wrong. Using sizeof(str2) means size of str2, which is a pointer, which, in 32-bit architectures, will be 32 bits. This is obviously not what you want.

3
  • If he wants to display the ASCII code then the code is correct. Small integer parameters passed to the printf-ish functions are integer promoted. Since the OP expects to print the value of '\0', printing as char is not particularly useful.
    – Lundin
    Commented Sep 27, 2012 at 14:22
  • Hi, thanks for the answer, it prints "hello" with %c but it now it's not printing the zeros, is there no way to print them both? Commented Sep 27, 2012 at 14:58
  • No, there's no way. Anyway, why you want to print a 0? When the string ends, it's a zero :)
    – m0skit0
    Commented Sep 27, 2012 at 15:41
1

The reason you are not seeing the characters is because the printf is printing integers. Change it to %c:

fprintf(stdout, "str2[%u]: %c\n",i,str2[i]);
4
  • How do you get an upvote while my answer is the same and answered before you? Just curious :)
    – m0skit0
    Commented Sep 27, 2012 at 14:18
  • Copy-paste: your own worst enemy :)
    – m0skit0
    Commented Sep 27, 2012 at 14:22
  • @m0skit0: The votes on this stuff are largely a mystery to me. One cut-n-paste and you sometimes get 5 votes. But write a superb answer for another question with funny anecdotes, links to supporting information, and solve cold fusion, and you don't get a single vote. I'll vote yours up to re-balance some. Commented Sep 27, 2012 at 14:42
  • Yeah, so you're just like me then ^^ Thanks for your comments
    – m0skit0
    Commented Sep 27, 2012 at 15:43
0

This:

memset(str2,0,sizeof(str2));

is not needed, since this:

while (fscanf(file, "%s", str1) != EOF) 
{
 strcpy(str2,str1);
}

overwrites str2 with str1 for each iteration. This means the previous string is thrown away. It's possible that you meant strcat(), in which case you should have str2[0] = '\0';` before the loop.

0

Your goal, if I understand correctly, is to have a character array which will have the single line from a file in it, and 0's besides that. For example:

File:

hello

Array result:

[h][e][l][l][o][0][0][0]...

So the easy way to do that is start by init your array to 0's, then read the line out:

char str1[100] = {'0'};
fgets(str1, 100, file); 

To print them you can print it like the string it is:

printf("%s\n", str1); 

But keep in mind you set the whole thing to 0 (null terminators) so you'll only see "hello". If you print with decimals as you were doing:

for(i=0; i<100; i++)
  printf("%d ", str1[i]);

You'll see hello in ASCII followed by the 0's

104 101 108 108 111 10 0 0 0 0 0 0 0 0 0...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.