cannot extract name from file

This is a discussion on cannot extract name from file within the C Programming forums, part of the General Programming Boards category; hi all i am having a few difficulties as i cannot extract a name from a file i am reading ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    cannot extract name from file

    hi all

    i am having a few difficulties as i cannot extract a name from a file i am reading in

    i have the following code at the moment but i am not sure why it is not working, maybe ive used the wrong function
    i am not using the fread, fopen, fwrite statements.

    Code:
      infiled = open(argv[argc-1], O_RDONLY, 0);     //opens the file
    
        while (infiled, buffer < "¥", infiled++) 
        nread=read(infiled, buffer, sizeof (buffer));      //while the symbol is not reached then keep reading
    ideally i want it to got through each character and read it to see if is the special character, if it isnt then i would like to store it in the buffer

    all help is much welcome and appreciated

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to scan through the read buffer... something like strchr(buffer, '¥')

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by nonoob View Post
    You need to scan through the read buffer... something like strchr(buffer, '¥')
    but how does buffer know what it is reading
    should i not take off the while loop?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The read function will read sizeof(buffer) at a time. You need to make sure that buffer has a terminating null before applying the strchr(). If the file has more characters than the read size, then yes, the while loop needs to keep going. The way you have it currently is incorrect.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by nonoob View Post
    The read function will read sizeof(buffer) at a time. You need to make sure that buffer has a terminating null before applying the strchr(). If the file has more characters than the read size, then yes, the while loop needs to keep going. The way you have it currently is incorrect.
    thanks for your help but now im stuck with a whole load of errors
    i removed the while and read statement completely as they were not needed
    and replaced it using

    Code:
    L28 char *position = strchar(infiled, '¥');
    if (position)
        {
    L34      nwrite = open(position-infiled, O_CREAT, 0);
        }
    and i get the following errors

    :28:38: warning: multi-character character constant
    : In function ‘main’:
    :34: warning: passing argument 1 of ‘open’ makes pointer from integer without a cast
    /usr/include/fcntl.h:73: note: expected ‘const char *’ but argument is of type ‘int’
    /tmp/ccmeDwdt.o: In function `main':
    I dont actually know what i am missing now!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Like the error says, you've got a parameter wrong.

    Can you post up more of this code? It's like trying to read a book, but only by looking through a straw, with one eye.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by Adak View Post
    Like the error says, you've got a parameter wrong.

    Can you post up more of this code? It's like trying to read a book, but only by looking through a straw, with one eye.
    hmm i like the description

    Code:
    if (argc==2)
      {
      infiled = open(argv[argc-1], O_RDONLY, 0);
      }
      if (infiled == -1)
      {
        printf("%s cannot be opened\n", argv[argc-1]);
      }
      else
      {
        char *position = strchar(infiled, '¥');
        if (position)
        {
          nwrite = open(position-infiled, O_CREAT, 0);
        }
      }

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Wow you really need to work on understanding concepts. fopen() should only take the file name. Then you will need a loop to do the reads.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    i am focusing on using open rather than fopen

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    That's fine. But you should have ONE open.... Then in a loop do the reads.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    but the second open is creating the file
    i have to get the file created before i do any writes

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if (argc==2)
      {
      infiled = open(argv[argc-1], O_RDONLY, 0);
      }
      if (infiled == -1)
      {
        printf("%s cannot be opened\n", argv[argc-1]);
      }
      else
      {
        char *position = strchar(infiled, '¥');
        if (position)
        {
          nwrite = open(position-infiled, O_CREAT, 0);
        }
      }

    You're trying to use strchr() with a file pointer. It won't work (at least, not in my compiler). It requires a pointer to a string (a char array will do), but not a file pointer.

    So, you need to do some looping and take the rows of text into a char array, and nothing does that as nicely as fgets(buffer, sizeof(buffer), filePointer);

    Then have strchr() check each buffer.

    and loop back for the next row of text, etc.

    Code:
    while((fgets(buffer, sizeof buffer, filePointer)) != NULL) {
      //rest of your code in here
    }
    Is a good way to go, imo.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    hmm ok i see where your coming from but my issue is i am not using the fgets so im going to have to take the basics ac interpret it into a open statement

    i want to read in the text until it hits '¥' and then make a file called what it read

    i hope that makes sense

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by aadil7 View Post
    hmm ok i see where your coming from but my issue is i am not using the fgets so im going to have to take the basics ac interpret it into a open statement

    i want to read in the text until it hits '¥' and then make a file called what it read

    i hope that makes sense
    However you open the file, that makes no difference to the logic, later on. You still have to follow the steps, in order:

    1) Open the file - however you want to do it, doesn't matter.
    2) Put some number of char's into a char array (or even a single char).
    3) Then test the char or chars to see if it's the one you're looking for.

    This is an example of what I mean:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 512
    
    int main(int argc, char *argv[]) {
      int i,j, n; 
      char s[MAX];
      FILE *fp, *fp2;
    
      if(argc < 2) {
        printf("<< Restart and enter the filename >>");
        printf("\nPress enter when ready");
        (void) getchar();
        return 0;
      }
      fp=fopen(argv[1], "r");
      printf("\n\n\n");
      for(i=0;i<MAX;i++) {
        s[i]=fgetc(fp);
        if(s[i]=='¥') {  //ascii 157
          s[i]='\0';     //add the end of string marker
          fp2 =fopen(s, "w");
          if(fp2==NULL) {
            printf("Error opening output file\n");
            return 0;
          }
          fprintf(fp2, "When a lovely flame dies, smoke gets in your eyes.");
          break;
        }
      }  
      if(i==MAX)
        printf("\nChar %c was not found", 157);
      else
        fclose(fp2);
      fclose (fp);
      
      printf("\n\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }
    You may not want to even use strchr(), because you can do it, yourself, so easily.
    Last edited by Adak; 12-09-2010 at 07:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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