Thread: I need to read a line's end properly. Blank area problem.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    4

    I need to read a line's end properly. Blank area problem.

    I have an input text to read with fscanf function. I can read some of lines. But, I could not read a line. Can you help?
    My input.txt:

    54,ZeynepKucuk,62-46-59
    62,OsmanBuyuk
    46,GulayEr,25-23-32-14



    I can read first and third line. But I have some problem with second line because its third position is empty.
    But I can read surely if the input is like below


    54,ZeynepKucuk,62-46-59
    62,OsmanBuyuk,0
    46,GulayEr,25-23-32-1

    Some part of my code below
    Code:
    int id;
    char name[100];
    char friends[100];
    FILE*dosya = fopen("input.txt","r");
    struct node *root = NULL;
    while(fscanf(dosya,"%d,%[^,],%s,%[^,],%s\n",&id, name, friends)==3){
        root = insertNewUser(root, id, name, friends);
    }



    After adding to tree my datas and print, expected output is:

    54,ZeynepKucuk,62-46-59
    62,OsmanBuyuk
    46,GulayEr,25-23-32-1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,319
    You should read a whole line using fgets in the first instance.

    Code:
    char buff[BUFSIZ];
    while ( fgets(buff, sizeof(buff), dosya) != NULL ) {
        // do stuff
    }
    Then you can do things like this.
    Code:
    if ( sscanf(buff,"%d,%[^,],%s,%[^,],%s",&id, name, friends)==3) {
    }
    else if ( sscanf(buff,"%d,%[^,],%s,%[^,]",&id, name)==2) {
    }
    The advantage is that you can deal with the string in memory in many more ways than you can just reading from a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,715
    Someone correct me if I'm wrong, but I think scanf returns the number of variables it managed to read for a reason. That reason is that if it fails to read, say, the third one, it has already read the previous two. So you only need to detect when it returns 2 instead of 3, and set a default value for friends.

    Also, I'm not so sure your code works the way you think it does, because you need to add an asterisk ("%*[^,]" for example) to ignore input, as well as that your commas are doubled.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    Thanks for your response to my question.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    Can you explain clearly please? Is it possible to talk in private?

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    436
    Quote Originally Posted by GReaper View Post
    Someone correct me if I'm wrong, but I think scanf returns the number of variables it managed to read for a reason. That reason is that if it fails to read, say, the third one, it has already read the previous two. So you only need to detect when it returns 2 instead of 3, and set a default value for friends.

    Also, I'm not so sure your code works the way you think it does, because you need to add an asterisk ("%*[^,]" for example) to ignore input, as well as that your commas are doubled.
    Correct in both cases. But I wouldn't use fscanf() to read the values since scanning the second line will return 3, anyway (62, "OsmanBuyuk" and "46"). Unless agahpar follows Salem's and your recomendations... I prefer to use strtok to separate the substrings...

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    4
    Thank you all. I did it correctly. Special thanks to Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blank line within a #define
    By eddyq in forum C Programming
    Replies: 3
    Last Post: 03-05-2016, 12:55 AM
  2. Search for Blank Line
    By uconnhuskies in forum C Programming
    Replies: 9
    Last Post: 07-21-2011, 08:09 PM
  3. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  4. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  5. testing for blank line
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 09:50 PM

Tags for this Thread