Thread: help me pro: fgetc get the number from txt and strcat to compile it problem

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Unhappy help me pro: fgetc get the number from txt and strcat to compile it problem

    ========orderStudent.txt=========
    student01 99
    student02 88
    student03 77
    ===================

    Code:
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
          FILE *fp;
          fp = fopen("orderStudent.txt","r");
          
          char ch;
          int showTxt =-1;
          char marks[200] = {'0','0','0'};
          int count;
          char str1[] = "hello";
          char str2[] = " 123";
          char str3[] = "str2:";
          
         while(  (ch = fgetc(fp))!=EOF   )
          {      
                 if(ch == ' ')           {showTxt = 0;
                       count=0;
                 }
                 
                 if(ch == '\n')          {
                      strcat(str1,str2);          // correct
                      strcat(str3,marks[2]);      // incorrect
                       printf("\t%s\n",str3); 
                       printf("\t%c%c",marks[1],marks[2]);
                       showTxt = -1;              }
    
          if((showTxt ==0)&&(count>0))   {            marks[count] = ch;
          printf("%c",marks[count]);           
                                               }
    
          ++count;
          }
          fclose(fp);
          printf("\n\n");
          
          system("PAUSE");
          return 0;
          }
    25 C:\..........countStudent2.c [Warning] passing arg 2 of `strcat' makes pointer from integer without a cast


    ================================================== ===========================

    hello everyone , This is my first post on this forum.
    I am a student learning C,

    I know what happen to the program, but i don't know how to solve it

    marks[2] - make the problem, it is a single number got from the txt file, such as 8,7

    but...
    1. char ch; ==> then ch is in type of char?
    2. so, ch = fgetc(fp) , all ch will be in char type?
    3. so, when marks[2] = ch, not matter marks[2] is something, such as 1,2,3,4,... all should be char

    from 1-3 , is what i think. any thing wrong?

    if marks[2] is type char?
    why ? strcat(str3,marks[2]); will make the program error

    tell you more, as before i test , marks[2] is a int.

    Please anyone can help me ?
    but don't change to much for the program if it is not necessary, unless the program work but i don't understand.

    i know if marks[2] is a char , then all problem is solved.

    help me pro.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Yes.
    2. Uh. fgetc returns an integer, which you are then forcing into a char variable. This will work except perhaps when fgetc returns EOF, and that might work too. (It doesn't have to work, but it might.)
    3. marks[2] is a char -- you defined it as such. So it might be '1', or '4', or whatever as a char.

    Now, you can only strcat a string onto a string. Since marks[2] is not a string but a char, you can't strcat it on.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strcat(str1,str2); // correct

    this is incorrect also - str1 does not have enough room for appending additional characters
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Question a new question

    tabstop , thanks a lot

    should i make for the question's conclusion is that if is need to use strcat() then strcat(char*dest, const char*src) , in the strcat(A,B) , the A && B both should be string ? right ? not single char, such 'a' , but "a" is correct right?

    also the program use ch = fgetc , that mean get the char from txt one by one , then to strcat() to compile the char is impossible? right?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    vart hello ,

    strcat(str1,str2); is wrong? but when i execute the file , this will not be error ?

    could be please tell me more the it , i want to learn is ?

    please give me an example . i run from Dev-C++

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    for my third question , from the top post ,

    if i want to get the char from the txt file, such as the first line, 9,9 , i want make it in 99, als it is countable

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Both sides of strcat must be a string, yes; and the first one must be something writable and have room. Your str1, str2, and str3 are writable, but they do not have room.

    If you want to get something character-by-character, you probably want to make a char buffer[large_number] and then read a character into buffer[0], buffer[1], buffer[2], .......

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    ok . i understand . however, i don't how to get the students' marks and also compare the marks

    as i need to see which one is the higest

    please tell me how to do , or give me tips

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can read directly into an int variable with fscanf, you know.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char buf[BUF_SIZ];
    while(fgets(buf, sizeof buf, fp))
    {
       char name[256];
       int mark;
       if(sscanf(buf, "&#37;255s %d", name,&mark) == 2)
       {
           printf("Student name: <%s>, Mark - %d", name,mark);
       }
       else
       {
          printf("Failed to parse line <%s>", buf);
       }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed