Thread: file i/o and remove something

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    file i/o and remove something

    Hi,

    i have a problem of removing some text from a file between this characters <>. For example:

    input file
    #include <stdio.h>

    output file
    #include <>

    I did this so far:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	
    	FILE *vData, *iData;
    	int c;
    	
    	vData=fopen("test.txt", "rt");/*vhodDatoteka*/  // input file
    	iData=fopen("test01.txt", "wt");/*izhodDatoteka*/ // output file
    	
    	while((c=fgetc(vData))!=EOF){  //reading the character
    		putc(c, iData);            // copying characters to output file
    		/*if(c=='<'){
    		
    	}*/
    	}
    	
    	fclose(vData);
    	fclose(iData);
    	
    	return 0;
    }
    And this is where iam stuck. I dont know how to countinue. Can anyone please give me some pointers how to solve this.

    Thank you.
    Last edited by bnkslo; 05-17-2008 at 09:05 AM.

  2. #2
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Code:
    if c == '<':
        skip this '<'
        skip until you read '>'
        skip this '>' also
        start copying again

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    I thought of that. But what if i have in the text:

    input file
    for(i=0;i<15;i++)

    output file
    for(i=0;i<..... where does it end ?

    So i think i cant just stop copying with this character < and countinue when i read character > ? I have to find the end character first i think ?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    When you fall on the '<', store the characters read in a buffer. If you reach the end of file or another '<', then you can flush the buffer. If the matching '>' is found, you can clear the buffer and resume the output. It also depends on what you wish to do with nested sequences <...<...>...> etc.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You're correct - you can't just start with <.
    If you're only looking at includes, just make sure the line starts with #include, then, for the rest of the line, do your replacement.

    Quote Originally Posted by root4
    When you fall on the '<', store the characters read in a buffer. If you reach the end of file or another '<', then you can flush the buffer. If the matching '>' is found, you can clear the buffer and resume the output. It also depends on what you wish to do with nested sequences <...<...>...> etc.
    What about:
    Code:
    for(; a < 3 && b > 5; ++a, ++b)
    Since this is doable by a simple regex - is a program really needed?
    Last edited by Cactus_Hugger; 05-17-2008 at 09:30 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Originally Posted by Cactus_Hugger
    Since this is doable by a simple regex - is a program really needed?
    It's for homework.

    Another question. How can you go back to the first character ('<') when i found the last one ('>')?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why would you?
    But to answer it: keep track of where you found the '<' (store it in a variable somewhere). Then you would know where that position is, so you can revert to it later.
    Though you might have nested '<', I think it's best to start off small and assume it's not nested.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    What about:
    Code:
    for(; a < 3 && b > 5; ++a, ++b)
    At this point (if the code above should not be interpreted as a normal <...> sequence), you need a complete C parser to make the difference (as this a semantical problem)...
    This constraint was not precised in the 1st post.

    EDIT: in fact the real question is: what are the characters allowed between < and > ? alpha numeric characters only? spaces too? anything? depending on that the solution would be different.
    Last edited by root4; 05-17-2008 at 11:54 AM.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Originally Posted by root4
    in fact the real question is: what are the characters allowed between < and > ? alpha numeric characters only? spaces too? anything? depending on that the solution would be different.
    I have to copy the characters from input file to the output file. But in the process i have to remove everything that is between the characters <>...

    Example:

    input file

    Today is a <sunny> day.

    The output file should look like this.

    Today is a <> day.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You just need to remember when a '<' is found and skip all characters from there until you find a '>'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    root4 wrote
    When you fall on the '<', store the characters read in a buffer. If you reach the end of file or another '<', then you can flush the buffer. If the matching '>' is found, you can clear the buffer and resume the output. It also depends on what you wish to do with nested sequences <...<...>...> etc.
    Some questions?

    1. How much buffer? Only for one char or do i allocate for the whole file?
    2. I was reading about allocating memory but I'am still confused?

    Can i do something like this?

    Code:
               char *buffer;
    
               buffer=malloc(sizeof(vData)); // or sizeof(char)
    3.Can I use buffer now like a variable? Like put char in it with putc or something?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. How many characters do you think there will be between < and >?
    2. The thing inside malloc is how many characters you want. See #1.
    3. buffer is a variable, but it is not a char variable. It points to a char. It would be more analogous to char foo[50], than char foo.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    tabstop
    1. How many characters do you think there will be between < and >?
    2. The thing inside malloc is how many characters you want. See #1.
    3. buffer is a variable, but it is not a char variable. It points to a char. It would be more analogous to char foo[50], than char foo.
    1. Well that is the question? It can be 1 or maybe 100. That's something i can't predict. Or it could happen that when i find < there is not a > character and i get to EOF. So i don't have a number!?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Exactly.
    So if you don't have the numbers of characters, then how would you proceed?
    Think of a real life situation. If you can't find anymore inside something, then what do you need to do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    I would say i need so much memory as the file is big? But i could be wrong!?

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. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM