0

My problem is when I try to save the string (series[0]) Inside (c[0]) and I display it, it always ignore the last digit.

For Example the value of (series[0]) = "1-620" So I save this value inside (c[0])

and ask the program to display (c[0]), it displays "1-62" and ignores the last digit which is "0". How can I solve this?

This is my code:

#include <stdio.h>
int main(void)
{
    int price[20],i=0,comic,j=0;

    char name,id,book[20],els[20],*series[20],*c[20];

    FILE *rent= fopen("read.txt","r");

    while(!feof(rent))
    {
        fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);
        printf("1.%s %s  %s  %d",&book[i],&els[i],&series[i],price[i]);
        i++;
    }

    c[0]=series[0];

    printf("\n%s",&c[0]);

    return 0;
}
Allen
  • 162
  • 2
  • 3
  • 15
Adel
  • 3
  • 2
  • 4
    pointers need to point somewhere, or you get undefined behavior. – Peter Jun 09 '16 at 23:39
  • To expand on the previous comment. `&series[i]` is a pointer to a `char *`. That is, it is a `char **`. That means it only has space for a single `char *` to be written. Clearly not what you want. – kaylum Jun 09 '16 at 23:50
  • 2
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for why the loop control is wrong. Always test the result of `fscanf()` before using it. – Jonathan Leffler Jun 10 '16 at 00:11

2 Answers2

0

The use of fscanf and printf is wrong :

fscanf(rent,"%s%s%s%d",&book[i],&els[i],&series[i],&price[i]);

Should be:

fscanf(rent,"%c%c%s%d",&book[i],&els[i],series[i],&price[i]);

You have used the reference operator on a char pointer when scanf expecting a char pointer, also you read a string to book and else instead of one character.

printf("1.%s %s  %s  %d",&book[i],&els[i],&series[i],price[i]);

Should be:

printf("1.%c %c  %s  %d",book[i],els[i],series[i],price[i]);

And:

printf("\n%s",&c[0]);

Should be:

printf("\n%s",c[0]);

c is an array of char * so c[i] can point to a string and that is what you want to send to printf function.

*Keep in mind that you have to allocate (using malloc) a place in memory for all the strings you read before sending them to scanf:

e.g:

c[0] = (char*)malloc(sizeof(char)*lengthOfString+1);

and only after this you can read characters in to it.

or you can use a fixed size double character array:

c[10][20];

Now c is an array of 20 strings that can be up to 9 characters long.

Dr.Haimovitz
  • 1,568
  • 12
  • 16
  • It seems a little unlikely that books have single-character titles, or that series names are single characters, etc. So, I don't think your suggested change is actually the appropriate one. – Jonathan Leffler Jun 10 '16 at 00:12
  • Im my answer i fixed the programming errors, the person who asked the question can now understand what he did wrong and change the code according to his needs. – Dr.Haimovitz Jun 10 '16 at 00:17
  • 1
    "c is an array of strings (char*)" -- No, C is an array of *pointers*, each of which may be a pointer to a string. Strings are not pointers. A "string" is by definition "a contiguous sequence of characters terminated by and including the first null character". – Keith Thompson Jun 10 '16 at 01:14
0

Amongst other problems, at the end you have:

printf("\n%s",&c[0]);

There are multiple problems there. The serious one is that c[0] is a char *, so you're passing the address of a char * — a char ** — to printf() but the %s format expects a char *. The minor problem is that you should terminate lines of output with newline.

In general, you have a mess with your memory allocation. You haven't allocated space for char *series[20] pointers to point at, so you get undefined behaviour when you use it.

You need to make sure you've allocated enough space to store the data, and it is fairly clear that you have not done that. One minor difficulty is working out what the data looks like, but it seems to be a series of lines each with 3 words and 1 number. This code does that job a bit more reliably:

#include <stdio.h>

int main(void)
{
    int price[20];
    int i;
    char book[20][32];
    char els[20][32];
    char series[20][20];
    const char filename[] = "read.txt";

    FILE *rent = fopen(filename, "r");
    if (rent == 0)
    {
        fprintf(stderr, "Failed to open file '%s' for reading\n", filename);
        return 1;
    }

    for (i = 0; i < 20; i++)
    {
        if (fscanf(rent, "%31s%31s%19s%d", book[i], els[i], series[i], &price[i]) != 4)
            break;
        printf("%d. %s  %s  %s  %d\n", i, book[i], els[i], series[i], price[i]);
    }

    printf("%d titles read\n", i);

    fclose(rent);

    return 0;
}

There are endless ways this could be tweaked, but as written, it ensures no overflow of the buffers (by the counting loop and input conversion specifications including the length), detects when there is an I/O problem or EOF, and prints data with newlines at the end of the line. It checks and reports if it fails to open the file (including the name of the file — very important when the name isn't hard-coded and a good idea even when it is), and closes the file before exiting.

Since you didn't provide any data, I created some random data:

Tixrpsywuqpgdyc Yeiasuldknhxkghfpgvl 1-967  8944
Guxmuvtadlggwjvpwqpu Sosnaqwvrbvud 1-595  3536
Supdaltswctxrbaodmerben Oedxjwnwxlcvpwgwfiopmpavseirb 1-220  9698
Hujpaffaocnr Teagmuethvinxxvs 1-917  9742
Daojgyzfjwzvqjrpgp Vigudvipdlbjkqjm 1-424  4206
Sebuhzgsqpyidpquzjxswbccqbruqf Vuhssjvcjjylcevcisdzedkzlp 1-581  3451
Doeraxdmyqcbbzyp Litbetmttcgfldbhqqfdxqi 1-221  2485
Raqqctfdlhrmhtzusntvgbvotpk Iowdcqlwgljwlfvwhfmw 1-367  3505
Kooqkvabwemxoocjfaa Hicgkztiqvqdjjx 1-466  435
Lowywyzzkkrazfyjuggidsqfvzzqb Qiginniroivqymgseushahzlrywe 1-704  5514

The output from the code above on that data is:

0. Tixrpsywuqpgdyc  Yeiasuldknhxkghfpgvl  1-967  8944
1. Guxmuvtadlggwjvpwqpu  Sosnaqwvrbvud  1-595  3536
2. Supdaltswctxrbaodmerben  Oedxjwnwxlcvpwgwfiopmpavseirb  1-220  9698
3. Hujpaffaocnr  Teagmuethvinxxvs  1-917  9742
4. Daojgyzfjwzvqjrpgp  Vigudvipdlbjkqjm  1-424  4206
5. Sebuhzgsqpyidpquzjxswbccqbruqf  Vuhssjvcjjylcevcisdzedkzlp  1-581  3451
6. Doeraxdmyqcbbzyp  Litbetmttcgfldbhqqfdxqi  1-221  2485
7. Raqqctfdlhrmhtzusntvgbvotpk  Iowdcqlwgljwlfvwhfmw  1-367  3505
8. Kooqkvabwemxoocjfaa  Hicgkztiqvqdjjx  1-466  435
9. Lowywyzzkkrazfyjuggidsqfvzzqb  Qiginniroivqymgseushahzlrywe  1-704  5514
10 titles read
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278