Thread: File and text manipulation

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    File and text manipulation

    Hi

    first of all I didn't write this thread till I tried million times with this piece of code but still I'm not getting the desired results.

    I have a textfile and i want to exchange the lines,
    ex:
    there
    Hi

    to:
    Hi
    there

    I'm not trying to edit the same file, I'm trying just to print the wanted result to the screen
    here is my code and tell me what's wrong (no compiling error or warnings)

    Code:
    void revlines(int my_fd)
    {
    int r=0; // to be the index of the end of a new line
    int s=0; 
    int l=0; // to be the index of the beginning of the line
    char line[600];
    char buf[600];
    int j=0;
    int i=2;
    lseek(my_fd,-2,SEEK_END);
    do
    {
    	read(my_fd,buf,1);
    	line[j]=buf[0];
    	j++;
    	i++;
    	if(line[j]=='\n')
    	{	l=r;
    		r=j;
    		for(s=r+1;s>l-1;s--)
    		printf("%c",line[s]);	
    	}
    }while(lseek(my_fd,-i,SEEK_END)!=-1);
    it just prints the last line at first(which is good) then stops working, even if there's more than 2 lines in the file.
    this may be an easy problem for you guys but this is just my second program in c and this is just a tiny bit of it.
    Thanks in advance
    Last edited by A.C Milan; 01-03-2009 at 12:02 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to store the lines somewhere, since you overwrite each line with the one after it. (Or at least I think that's what you're trying to do.)

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    I am storing them in line[ ].
    the idea is that I'm reading backwards from the end of the file, then each char read wil be inserted to the line array until i encounter a '\n', thats when i will print what is in the line array backwards too, and i will do it again till the file finishes(reaches the beginning in our case)
    but there is something wrong in the code i guess,

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, there's probably not a \n at the start of the file, so the first line won't get written out.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think what you've got is unnecessarily complex and obviousy isn't accomplishing what you want. You're reading the lines in backward, and storing them character by character in line, okay. But when you want to print the line correctly, you need to count back through your buffer in the correct place, and I'm not so sure that's happening here. Perhaps you need a counter that gets reset to zero at every new line (for length), and another variable containing the start of the current line (for offset).

    Why don't you add in some comments explaining what all these ints and assignments were supposed to accomplish and it will be easier to tell where you've gone wrong.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Well, there's probably not a \n at the start of the file, so the first line won't get written out.
    well yes you're right, what i know is that the file will begin with a ^ and end with a $ (I'm working under linux)
    but i was doing that when programming in BASH, will this be correct also in C?

    I think what you've got is unnecessarily complex and obviousy isn't accomplishing what you want. You're reading the lines in backward, and storing them character by character in line, okay. But when you want to print the line correctly, you need to count back through your buffer in the correct place, and I'm not so sure that's happening here. Perhaps you need a counter that gets reset to zero at every new line (for length), and another variable containing the start of the current line (for offset).

    Why don't you add in some comments explaining what all these ints and assignments were supposed to accomplish and it will be easier to tell where you've gone wrong.
    __________________
    OK I'll edit the code and show comments ...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by A.C Milan View Post
    the file will begin with a ^ and end with a $
    Those are regular expression operators, so no. If you feed the entire thing into line backward, then I guess it will begin at line[strlen(line)-1] and end at line[0]. There is an EOF (end-of-file) special character* but no "beginning-of-file" marker.

    *actually it's not really a character, it maybe some kind of multibyte sequence? Or just an illusion produced by some c functions? Maybe someone who knows will tell...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    I would consider using a stack to save the lines you read from the file. That way you can read the file from the beginning, pushing lines on the stack as you go. Later, when you want to print the file in reverse you pop the lines from the stack, one by one, and print them until the stack is empty.

    I think that this would be more robust and less error prone as you can read the file from beginning to end using, for example, fgets instead of seeking back and forth in the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM