Thread: How come I can print the entire string but, not an individual character?

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    How come I can print the entire string but, not an individual character?

    Hi Guys, I'm trying to parse a log that looks like this:

    Code:
    data,b1a571,3324 d,123.46,987.65
    [2022-09-01 16:08] ,RSSI: -25
    [2022-09-01 16:08] ,Sent a reply
    [2022-09-01 16:08] ,mdata,b1a571,3324 d,123.46,987.65
    [2022-09-01 16:08] ,RSSI: -25
    [2022-09-01 16:08] ,Sent a reply
    [2022-09-01 16:08] ,mdata,b1a571,3324 d,123.46,987.65
    [2022-09-01 16:08] ,RSSI: -25
    [2022-09-01 16:08] ,Sent a
    Notice the first and last lines are often partial entries. If the first line doesn't begin with a '[', I intend to append it to the last line of the prior log.

    If the last entry ends without a '\n', I'm planing to save it to a temporary string and append the first line of the next log. Am I making sense? I'm a novice C coder but, really enjoying it.

    Here's my code that fails to figure out if the first line begins with a '['.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    // How to read a text file line by line in C Programming - YouTube
    
    
    char fpath4[100] = "C:/work/arduino/teraterm-4.106/logs/";
    char fname4[] = "logToParse";
    
    
    void main(){
        strcat(fpath4, fname4);                     // put the log name at the end of the path
        FILE *the_file = fopen(fpath4, "r");        // open the file Read Only
        if(the_file == NULL){                       // if the file doesn't exist, display an error
            perror("\nUnable to open the file");
            exit(1);                                // exit the program
        }
        char line[100];
        //while(fgets(line, sizeof(line), the_file)){ // while there is a line : fgets gets a single line
            fgets(line, sizeof(line), the_file);
            printf("\n%s", line);                   // prints the entire line of the log
            printf("\n%s", line[0]);                // DOES NOT WORK
        //}
        fclose(the_file);
        //fclose(???);
        return 0;
    }
    I'm try to read only the first character of 'line' to see if it is a '['. How come
    Code:
    printf("\n%s", line[0]);
    does not work? Do I need to convert 'line' to an array first or something? Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    doh!!! %c!

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Code:
    printf("%c", line[0]);
    %s, prints the entire string
    %c, prints one character

    Once again, please see my response in previous thread!

  4. #4
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Hey rstanley, Thank you for the response. I have read two books on C and purchased another today: Amazon.com I'm drinking from a fire hose! I forget most of what I'm reading but, still a lot sticks. Please forgive my novice questions. Believe me, I'm trying. It's just that I'm a bit thick in the head. I will be more careful.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by mpatters View Post
    Hey rstanley, Thank you for the response. I have read two books on C and purchased another today: Amazon.com I'm drinking from a fire hose! I forget most of what I'm reading but, still a lot sticks. Please forgive my novice questions. Believe me, I'm trying. It's just that I'm a bit thick in the head. I will be more careful.
    Good book for its time, but not current. It covers the C89/90 C Standard, but superseded by C99, C11, C18 (Sometimes listed as C17), and the next version of the C Standard "Cxx".

    The three books I listed are more up to date, and recommended over K&R, 2nd edition.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by rstanley View Post
    Good book for its time, but not current.
    By "its time" you mean 1988, or 34 years ago. That's an eternity in computers!

    (I noticed the edition listed on that Amazon page is published by Pearson India. There seems to be a trend of using old technology like Borland Turbo C for DOS in some Indian computer programming courses. Maybe they've upgraded to the C90 standard recently?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-02-2014, 09:41 AM
  2. replacing individual chars in a string array
    By Trekker182 in forum C Programming
    Replies: 7
    Last Post: 11-16-2013, 07:31 PM
  3. saving entire input into 1 string?
    By rodrigorules in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2010, 02:44 PM
  4. retrieving the entire string after using getline
    By bleuz in forum C++ Programming
    Replies: 8
    Last Post: 10-04-2009, 01:19 PM
  5. Scanf doesn't take in entire string
    By DragonX2n in forum C Programming
    Replies: 5
    Last Post: 01-31-2006, 09:30 PM

Tags for this Thread