Thread: [ask]read file and get substr

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Question [ask]read file and get substr

    hlow all ,

    iam begin in c programming so i need your advice

    i have sample.txt
    Code:
    308201a1a0108009090318035
    i want to read the file and get substr start 1 to 3 character from that file

    this my code but this still error
    Code:
     pFile = fopen(szFileName, "r");    char ch ;
        while ( 1 )
        {
           ch = fgetc ( pFile ) ;
           if ( ch == EOF )
           break ;
           printf ( "%c", ch ) ;
           char *to = (char*) malloc(6);
           strncpy(to, from+0, 3);
           printf("\nopening file: %s !", to);
    
           
        }
          fclose ( pFile ) ;
    i want get output like this
    Code:
    308
    any one can help me for fix this code

    thx for your advice and help

    br\\zvtral

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    1. what does 'from' contain? nothing as far as your code shows
    2. you only read one character from the file and then did not use it.
    3. your 'to' was malloc'ed for 6 characters but malloc doesn't set the data to 0 so it may not be 0's
    4. strncpy won't put the 0 delimiter at the end of 'to' if the character count limit (3) is reached. so you need to be sure you add a zero delimiter

    use fgets with 'from' as the destination buffer.
    do the strncpy
    add a 0 delimiter at to[3]

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    @dmh2000
    thx for your advice

    my code i get from internet so this code not correct
    can you explain about your advice
    i have fileinput example.txt
    Code:
    308201a1a0108009090318035
    so i want output can display only select substr

    output

    Code:
    820
    if i using shell script like this
    Code:
    export i=`cat example.txt`
    echo $i|awk '{print substr("'"$i"'",3,3)}'
    can you help me how do that with c code

    Quote Originally Posted by dmh2000 View Post
    1. what does 'from' contain? nothing as far as your code shows
    2. you only read one character from the file and then did not use it.
    3. your 'to' was malloc'ed for 6 characters but malloc doesn't set the data to 0 so it may not be 0's
    4. strncpy won't put the 0 delimiter at the end of 'to' if the character count limit (3) is reached. so you need to be sure you add a zero delimiter

    use fgets with 'from' as the destination buffer.
    do the strncpy
    add a 0 delimiter at to[3]

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    iam begin in c programming so i need your advice
    my code i get from internet so this code not correct
    If you want to learn C, you'd best start at the beginning and slowly work your way through the basics.

    The chances of "finding" good code on the internet that does exactly what you want are slim - knowledge of programming will help you understand/modify such code. However, the best thing you can do for yourself while learning is *not* simply taking code from elsewhere, but writing it yourself.

    can you help me how do that with c code
    You probably won't receive a complete program here, because that's not what we do. We assist people who have specific problems with their code.

    If you have some knowledge of C, then the following links can help guide you to a solution:

    C File I/O Tutorial - Cprogramming.com

    C Strings - Cprogramming.com

    It's usually best to determine the algorithm you need to solve your problem by hand on paper first. Start at the beginning, and take it step by step, noting everything you do to arrive at a solution. These notes can help you generate the code you are looking for.

    When you start to write code, just write little bits at a time, compile, and test. See this link:

    A development process

    When you get stuck on a particular problem, share your code here (be sure to use code tags) and ask specific questions, and you'll likely get good answers to guide you.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zvtral View Post
    Code:
    echo $i|awk '{print substr("'"$i"'",3,3)}'
    can you help me how do that with c code
    To replicate this in C, just read from stdin via getchar() and don't worry about fopen, etc.

    Code:
    int pos = 0;
    int c = EOF;
    while ((c = getchar()) != EOF) {
      printf("pos: %d, char: '%c'\n", pos, c);
      pos++;
    }
    Start with this basic program and modify it to do what the "awk" part of your pipeline does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  3. Substr
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-04-2006, 01:44 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

Tags for this Thread