Stupid question maybe??

This is a discussion on Stupid question maybe?? within the C Programming forums, part of the General Programming Boards category; As part of my project I need a line counter i am doing this by making the program counter how ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    28

    Stupid question maybe??

    As part of my project I need a line counter i am doing this by making the program counter how many time ASC 10 comes up this is then the amout of lines

    The problem im having with my code is when it gets to the end of the file it needs to store the last number of the counter so I can use this to say how many lines there are please help

    [Code/]

    while(1) { /* keep looping... */
    c = fgetc(file);
    if(c==10 && c!=EOF) {
    counter==counter++ ;
    printf("%d",counter);
    getchar();


    Unsure how to store last counter entry as a seperate integer
    Cheers

    Bazza

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >counter==counter++ ;
    You can change this to
    Code:
    counter++; //This is the same as counter = counter + 1;
    I do not really understand your question. Can you try to rephrase it.

    Here is a way to count the number of char broken up by their ascii value.
    Code:
    int CountChar(char Name[], char Find){
    	int array[256] = {0};
    	int c;
    	FILE *fp = fopen(Name, "r");
    
    
    	while( (c=fgetc(fp)) != EOF )
    	{
        	array[c]++;
    	}
    
    	fclose(fp);
    
    	return array[Find];
    }
    To find the number of new lines you would use
    Code:
    int Number = CountChar("FileName.txt", '\n');
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem im having with my code is when it gets to the end of the file it needs to store the last number of the counter so I can use this to say how many lines there are please help

    Code:
    while(1) { /* keep looping... */
        c = fgetc(file);
        if(c==10 && c!=EOF) {
            counter==counter++ ;
            printf("%d",counter);
            getchar();
    Ok, a few things:

    1) Your example doesn't have enough } for the number of { you use.

    2) You are using a comparison operator instead of an assignment.

    counter == counter++;

    Should be:

    counter++;

    There is no need to do the comparison at all. Or even the assignment. Just a simple "counter++;" does everything you need.

    [edit]
    Ah. Beat me to it.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    while(1) { /* keep looping... */
    c = fgetc(file);
    if(c==10 && c!=EOF) {
    counter==counter++ ;
    printf("%d",counter);
    getchar();
    Perhaps something more like this would work a bit better:
    Code:
    int lineCount = 0;
    char input[BUFSIZ];
    while ( fgets ( input, sizeof input, file ) != NULL )
      lineCount++;
    printf ( "There are %d lines in the file\n", lineCount );
    When fgets returns NULL then the end of the file has been reached. The only downside is that you have to use a buffer which can hold the length of each line in the file individually or fiddle around with fgets in an awkward manner so that you don't cut off lines. Usually this won't happen, but it's something to consider if your file is large and unwieldy.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 01:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21