Thread: Reading from file strange problem

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It sure is too bad no one else suggested any alternative to what you were doing in this thread.


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

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by quzah View Post
    It sure is too bad no one else suggested any alternative to what you were doing in this thread.


    Quzah.
    As we say in Greece,τελος καλα ολα καλα which means that we succeded in the end of story

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That sound you just heard was my post going right over your head.


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

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok,i put the terminating '\0' .However program is not correct again.I noticed that every time i read a line in the loop of while,if the position was before held by a letter and now it has to be overwritted by tab,causes the problem.
    e.g (the names are seperated by tab,but bellow i have put space)
    Code:
    Wood Gina
    Ray Vickie
    Cobb Dale
    Then these are going to be saved as
    Code:
    Wood Gina
    Rayψ Vickie
    Cobb Dale
    When i write Rayd instead of Ray everything is ok.When i write Ra instead of Ray i get
    Code:
    Wood Gina
    RaQψ Vickie
    Cobb Dale

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    That means you didn't do the terminating \0 very well.

  6. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    That means you didn't do the terminating \0 very well.
    Maybe you are right . This is my function to do the job.s points at the beggining of string,size is the length of name e.g. Ray has size_for_malloc=3
    Code:
    char *put_NULL_at_end(char *s , unsigned int size_for_malloc)
    {
    	s+=size-1;
    	s='\0';
    	return s;
    }

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    I don't know why you are subtracting one. Returning s could be dangerous depending on whether you actually use the return value, since at the end of the function s points to the null character.

  8. #23
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    I don't know why you are subtracting one. Returning s could be dangerous depending on whether you actually use the return value, since at the end of the function s points to the null character.
    It has to do with length of name.Maybe i have to erase -1.However with -1 or not the problem is the return.I am going to try to pass arg 1 as char**

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You don't really have to do that -- passing a char* is fine, since the individual char's themselves are editable, which is the point. But if you had been doing something like
    Code:
    name = put_NULL_at_end(name, 4);
    then the return would trash name.

  10. #25
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    void put_NULL_at_end(char **s , unsigned int size_for_malloc)
    {
    	*s+=size_for_malloc;
    	**s='\0';
    }
    Does this seems ok?

  11. #26
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    You don't really have to do that -- passing a char* is fine, since the individual char's themselves are editable, which is the point.
    ok then here is my function.
    Code:
    void put_NULL_at_end(char *s , unsigned int size_for_malloc)
    {
    	s+=size_for_malloc;
    	*s='\0';
    }
    Ok,that's right.I found 899 people and the target is 900 . Propably something minor i think,but i can handle it.
    Thank you very very much
    Last edited by std10093; 05-19-2011 at 05:31 PM.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    That should work, since I think we have size_for_malloc calculated correctly up above, and hopefully you are using it after every strncpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bus error reading file; strange i value
    By xenonscreams in forum C Programming
    Replies: 3
    Last Post: 09-17-2010, 10:45 AM
  2. strange file opening problem
    By ac251404 in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2006, 04:35 PM
  3. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  4. Strange file problem.
    By tilex in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2004, 12:32 AM
  5. A strange problem with creating a file
    By Waldo2k2 in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2002, 03:42 PM