locked
Get characters per line RRS feed

  • Question

  • Hi,

    I am reading a text file, seperated by commas.  How do I get the number of characters in a given line.

    I was using

    fgets(chrX,sizeof(chrX)-1,infile), to read each line

    Thanks

    Friday, October 7, 2011 12:50 PM

Answers

  • You don't need the -1.  Use strlen to count the number of characters.
    • Marked as answer by Rob Pan Friday, October 14, 2011 3:18 AM
    Friday, October 7, 2011 1:47 PM
  • Hi,

     

    According to your description, does “number of characters” mean the number of words?

     

    If you need to get the number of words, I suggest you can create a new function to parse the string of line. For example:

    int cout = 0;

    trim

    for(int I = 0; I < strlen(line); i ++)

    {

    if(line[i] == ‘ ’)

    cout++;

    }

     

    I hope my suggestion can help you to resolve your issue.

     

    Best Regards,

    Rob


    Rob Pan [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Rob Pan Friday, October 14, 2011 3:18 AM
    Tuesday, October 11, 2011 8:10 AM

All replies

  • You don't need the -1.  Use strlen to count the number of characters.
    • Marked as answer by Rob Pan Friday, October 14, 2011 3:18 AM
    Friday, October 7, 2011 1:47 PM
  • Hi,

     

    According to your description, does “number of characters” mean the number of words?

     

    If you need to get the number of words, I suggest you can create a new function to parse the string of line. For example:

    int cout = 0;

    trim

    for(int I = 0; I < strlen(line); i ++)

    {

    if(line[i] == ‘ ’)

    cout++;

    }

     

    I hope my suggestion can help you to resolve your issue.

     

    Best Regards,

    Rob


    Rob Pan [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Rob Pan Friday, October 14, 2011 3:18 AM
    Tuesday, October 11, 2011 8:10 AM