Thread: How to convert char read from file into string

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    Question How to convert char read from file into string

    #include <stdio.h>
    #include <errno.h>
    #include <string.h>


    int main(int argc, char **argv)
    {

    FILE *f;
    int c;
    errno = 0;

    /Check for no. of arguments and try opening file/
    if (argc == 1)
    {
    printf("There is no file selected.\n");
    printf("Usage: test <filename> <filename> \n");
    }
    else
    {
    for (int i = 1; i < argc; i++)
    {
    if((f = fopen(argv[i], "r")) == NULL)
    {
    printf("File: %s cannot be opened. \n", argv[i]);
    fprintf(stderr, "Open file %s failed: %s\n", argv[i], strerror(errno));
    }
    else
    {

    /read from text file as characters/
    while ((c = fgetc(f)) != EOF )
    {
    if(c == '"')
    {
    while((c = fgetc(f)) != '"')
    {
    putc(c, stdout);
    }

    Hi there....the above is what I have got so far. It checks for the number of arguments and open the files specified. The it reads from the text files that the file is specified.

    Objectives:
    To read the contents of the file, check validity for absolute reference to a file ie. hypertext starting with file:/
    eg <a href="file:/example">. Should be recursive as well

    Questions:
    I have managed to check for < > and read any text inside the < > but I need to extract anything that is after file:/. I tried using fgets as well...but I cant seems to filter out text that is after the file:/.

    Any help, suggestions and comments are welcome as I am still new with C.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When you've read a <, copy all following chars to a buffer until you find the (matching) >.

    Then call another routine to validate the contents of the buffer between the <> pair

    Code:
    void validate ( char *buff ) {
        printf( "buff=%s\n", buff );
    }
    This just prints the buffer you want to validate. I would suggest you leave it like this until your file reading loop is reliably extracting <> pairs from your input files.

    Once it is, then you can focus on making validate() do what it's supposed to do

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    there's a problem here:
    Code:
    while ((c = fgetc(f)) != EOF ) 
    { 
    	if(c == '"') 
    	{ 
    		while((c = fgetc(f)) != '"') 
    		{ 
    			putc(c, stdout); 
    		}
    	}
    }
    What do you think will happen when fgetc() returns EOF? It'll keep on going, that's what!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    8
    Hi Hammer.....

    I am not very sure what you are trying to tell me
    because it seems ok when I tested the codes out. It reads anything that is inside the " ". Could you please elaborate??

    Thank you in advance...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by cruxxe
    Hi Hammer.....

    I am not very sure what you are trying to tell me
    because it seems ok when I tested the codes out. It reads anything that is inside the " ". Could you please elaborate??

    Thank you in advance...
    What they're saying is this:
    Code:
    while ( a != something )
        while( b != something_else )
           do_whatever( )
    In this case, the first loop we're checking for EOF.
    However, we are not checking for EOF in the second loop.
    What will happen if the second loop encounteres EOF?

    Well, since it's not looking for it, it'll just continue on its merry way, reading until it encounteres whatever is supposed to make it stop. Once it stops, the other loop will continue, because it just does another read, and sees that this read isn't EOF, so it starts the second loop again. See the problem?

    You've passed EOF in the inner loop, so the original EOF check will never (or highly unlikely) end.

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

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    8
    while ((c = fgetc(f)) != '>')
    {
    sprintf(buffer, "%s");
    validate(buffer);
    if(c == EOF)
    break;
    }

    Thanks....well..this is how I check for EOF, is there any other better way?? This way it will still print the stuff that is after < and before EOF.

    And using sprintf, I attempt to read the characters into a char buffer[] but the result I get is quite gibberish.....why is that so?? Even when I use %s ??

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > because it seems ok when I tested the codes out.
    You're probably OK while your input files are well-formed HTML - that is, all the <> match up.
    However, you're more likely to come unstuck with a bad HTML file, which contains a <, and no following > before the end of file

    Here's my idea for building your buffer
    Code:
    #include <stdio.h>
    
    void validate ( char *buff ) {
        printf( "%s\n", buff );
    }
    
    int main ( ) {
        char buff[BUFSIZ];
        int  i = 0;
        int  ch;
        while ( (ch=fgetc(stdin)) != EOF ) {
            if ( ch == '<' ) {
                /* found a <, now find the > */
                while ( (ch=fgetc(stdin)) != EOF && ch != '>' ) {
                    /* also check i < BUFSIZ as well */
                    buff[i++] = ch;
                    buff[i] = '\0';
                }
                if ( i > 0 ) {
                    validate( buff );
                    i = 0;  /* ready for the next one */
                }
            }
        }
        return 0;
    }

    > Use fread() instead.
    fread() has no benefits over fgetc for reading text files

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    8

    Smile

    I can have some rest now.........proggie is working.

    Thanks everyone for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. cant read last char from a file
    By aze in forum C Programming
    Replies: 8
    Last Post: 04-25-2004, 05:20 PM