Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Stop searching at end of line - C Programming

  • 03-03-2009 11:00am
    #1
    Closed Accounts Posts: 2,696 ✭✭✭


    I need the below code to stop searching at the end of the first line - I have tried adding '\0' but still continues to the EOF

    Any assistance is very welcome


    #include <stdio.h>
    int main() {
    FILE *fp;
    int i;
    int colon=0;
    int line_num;
    int apostrophe;
    char a_single_character;
    fp = fopen("./happy_days.txt", "r");
    if(fp == NULL) {
    printf("Could not open file");
    return 1;
    }
    a_single_character = (char)fgetc(fp);
    line_num = 1;

    while(a_single_character != (char) NULL) {



    if (a_single_character == ':'){
    colon = colon + 1;

    //a_single_character = (char)fgetc(fp);
    }
    else if (a_single_character == '\''){
    apostrophe = apostrophe + 1;
    }
    a_single_character = (char)fgetc(fp);

    }
    printf("\n There are %d colons in the file", colon);
    printf("\n There are %d apostrophes in the file", apostrophe);

    return 0;
    }


Comments

  • Registered Users, Registered Users 2 Posts: 5,112 ✭✭✭Blowfish


    Shouldn't it just be '\n' you terminate on?


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Blowfish wrote: »
    Shouldn't it just be '\n' you terminate on?

    Perfect - Thanks

    Was getting caught up in Ascii :(


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    NB Windows files use \r\n for newlines while UNIX uses \n. If you ever opened a file in notepad that had squares instead of new lines that was UNIX newlines!

    Also if you surround your code on here with [ CODE] and [ /CODE] it will make it more readable


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    I still have a problem :(

    I thought it would be easy to once I could stop searching at the end of the line and continue to EOF but the file pointer doesnt seem to know that I want it to go to the next line :o

    What I need is to count the number of colons and apostrophes on each line and output the results - please help


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    #include <stdio.h>
    int main() {
        FILE *fp;
        int i;
        int colon=0;
        int line_num;
        int apostrophe;
        char a_single_character;
        fp = fopen("./happy_days.txt", "r");
    
        if(fp == NULL) {
            printf("Could not open file");
            return 1;
        }
    
        a_single_character = (char)fgetc(fp);
        line_num = 1;
    
        while(a_single_character != (char) NULL) {
            if (a_single_character == ':'){
                colon = colon + 1;
    
                //a_single_character = (char)fgetc(fp);
            }
            else if (a_single_character == '\''){
                apostrophe = apostrophe + 1;
            }
    
            a_single_character = (char)fgetc(fp);
        }
    
        printf("\n There are %d colons in the file", colon);
        printf("\n There are %d apostrophes in the file", apostrophe);
    
        return 0;
    } 
    

    Can you tell me where you're checking for the \n here? Is it "while(a_single_character != '\n') {"?

    Is it that you want to print out the number of colons and apostrophes on each line? If it's just the whole file you don't need to worry about lines, you can just do (I think) while(a_single_character != EOF)


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Was trying along the lines of below... Number of colons and apostrophes on each line is the required output




    #include <stdio.h>
    int main() {
       FILE *fp;
       int i;
       int colon=0;
       int line_num;
       int apostrophe;
       char a_single_character;
       fp = fopen("./happy_days.txt", "r");
     
    
      if(fp == NULL) {
           printf("Could not open file");
           return 1;
       }
     
       a_single_character = (char)fgetc(fp);
       line_num = 1;
       do{
       
         while(a_single_character != (char) '\n') {
          if (a_single_character == ':'){
           colon = colon + 1;
           //a_single_character = (char)fgetc(fp);
          }
          else if (a_single_character == '\''){
           apostrophe = apostrophe + 1;
          }
          a_single_character = (char)fgetc(fp);
         }
       }while (a_single_character != (char) NULL);
    
       printf("\n There are %d colons in the file", colon);
       printf("\n There are %d apostrophes in the file", apostrophe);
       return 0;
    }
    


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    1. You're only printing out the number in the file (at the end of the code). you need to print it out after each line.
    2. You also need to reset the colons and apostrophe counter, and increment line_number after doing each line:
         while(a_single_character != (char) '\n') {
          if (a_single_character == ':'){
           colon = colon + 1;
           //a_single_character = (char)fgetc(fp);
          }
          else if (a_single_character == '\''){
           apostrophe = apostrophe + 1;
          }
          a_single_character = (char)fgetc(fp);
         }
     
         printf("\n There are %d colons in line %d", colon, line_num);      
         printf("\n There are %d apostrophes in line %d", apostrophe, line_num);
         line_num = line_num + 1;
         colon = 0;
         apostrophe = 0;
    

    (The last 5 lines are new). Should be something like that anyway, I haven't tested it.


  • Closed Accounts Posts: 448 ✭✭ve


    I would have thought using fgets() would be a much quicker way of accomplishing this sort of thing.

    fgets will read a line from the file. Returns a pointer to a char array that contains the characters between the current file location and the next end of line character. It's been a while since I coded in C, but this may be a quick answer for you.


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Finished article below, though if there are blank lines in the file it seems to get stuck :confused: . However for my purpose this is not an issue. Thanks All
    #include <stdio.h>
    int main() {
     FILE *fp;
     int line_num;
     int colon = 0;
     int apostrophe = 0;
     char a_single_character;
     fp = fopen("./happy_days.txt", "r");
     if(fp == NULL) {
      printf("Could not open file");
      return 1;
     }
     a_single_character = (char)fgetc(fp);
     line_num = 1;
     do{
      while(a_single_character != (char)'\n') {
       if (a_single_character == ':') {
        colon = colon + 1;
       }
       else if (a_single_character == '\'') {
        apostrophe = apostrophe + 1;
       }
       a_single_character = (char)fgetc(fp);
      }
      //printf("\n There are %d colons in line %d", colon, line_num);      
      //printf("\n There are %d apostrophes in line %d", apostrophe, line_num);
      if (apostrophe > 1){
       printf("\nERROR ON LINE %d", line_num);
      }
      line_num = line_num + 1;
      colon = 0;
      apostrophe = 0;
      a_single_character = (char)fgetc(fp);
     } while (a_single_character != EOF);
     printf("\nProgram is currently set to count ' only");
     
    
     return 0;
    }
    


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    though if there are blank lines in the file it seems to get stuck

    know you dont need to worry about it, but remember that windows formats newlines as [PHP]0x0d,0xa[/PHP]

    if you had blank line
    line 1
    
    line 3
    line 4
    

    that would mean 4 bytes after "line 1": [PHP]0x0d,0x0a,0x0d,0x0a[/PHP]


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 378 ✭✭gagomes


    ve wrote: »
    I would have thought using fgets() would be a much quicker way of accomplishing this sort of thing.

    fgets will read a line from the file. Returns a pointer to a char array that contains the characters between the current file location and the next end of line character. It's been a while since I coded in C, but this may be a quick answer for you.

    The problem with fgets, is that it reads a fixed amount of characters.

    so, if you do something like:

    char buffer[256];

    fgets(buffer, sizeof(buffer), stream)

    and the the first line of the file is 300 chars long, you'll not be able to read the whole string. It's preferable to make a readline() which dynamically allocates a buffer and uses fgetc() and realloc. Here's a quick example which I just made out of boredom. I haven't done any C development in ages...but it's certainly one of my favorite languages. :-)
    #include <stdio.h>
    #include <stdlib.h>
    
    char *readline(FILE *stream)
    {
       char *ptr;
       char c;
       int size = 0;
    
       ptr = (char *) malloc(1);
    
       while ((c = fgetc(stream)) != '\n') {
             ptr[size++] = c;
             ptr = realloc(ptr, size);
       }
    
       ptr[size++] = '\0';
       ptr = realloc(ptr, size);
    
       return ptr;
    }
    
    
    int main(int argc, char **argv)
    {
        FILE *stream;
        char *p;
    
        if (argc <= 1)
            return 1;
    
        stream = fopen(argv[1], "r");
    
        p = readline(stream);
        printf("line 1 = %s\n", p);
        free(p);
    
        p = readline(stream);
        printf("line 2 = %s\n", p);
        free(p);
    
    
        fclose(stream);
    
        return 0;
    }
    
    example:

    $ ./foo /etc/passwd
    line 1 = root:x:0:0:root:/root:/bin/bash
    line 2 = daemon:x:1:1:daemon:/usr/sbin:/bin/sh


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Oooh fancy :p


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Martyr wrote: »
    if you had blank line
    line 1
     
    line 3
    line 4
    

    that would mean 4 bytes after "line 1": [php]0x0d,0x0a,0x0d,0x0a[/php]

    can this be done with a text file? fairly newbie to programming - I was of the understanding that for navigation of a file by memory size t had to be a .dat type file


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    All files are just binary :)

    Extensions are just a way for humans to identify a file type,
    and for the operating system to know what application to execute, processing data in the file.

    You can call a file anything you want, doesn't matter.(within rules of file system)
    its how you interpret the data in the file thats important.

    you may need to change the mode from "r" to "rb" (binary mode)
    but the data is still just binary..

    Rename a TXT file to MP3, and windows will use WMP to try read it, but won't find any recognised headers just giving an error.

    on linux, newlines only store '\n' or 0x0a, for windows its '\r\n' or 0x0d,0x0a - i don't know why your code wasn't working, didn't test it, but maybe its because of the way you identify newlines.


Advertisement