fputs doesn' t stop reading when

This is a discussion on fputs doesn' t stop reading when

Thread: fputs doesn' t stop reading when \0 is found?

is found?
within the C Programming forums, part of the General Programming Boards category; fputs doesn' t stop writing when

Thread: fputs doesn' t stop reading when \0 is found?

is found? in my code i have a file where i have 5-6 ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    fputs doesn' t stop reading when \0 is found?

    fputs doesn' t stop writing when \0 is found?

    in my code i have a file where i have 5-6 lines separated by enter and fputs "grabs" the whole line(it doesn' t stop at each word as i want)...why?
    Last edited by brack; 09-03-2010 at 10:18 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    fputs doesn't read, it writes. Are you getting mixed up between fputs and fgets?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    oh sorry for my mistake...i mean writes...thanks for correcting me...

    do you know why?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Enter (newline?) is not the same as \0 (NULL terminator). If the string you are writing have newline characters in them, fputs is not going to stop when it encounters them. It keeps writing until it reaches the \0. So, confirm whether or not you are talking about newlines or nulls in the source string and then also confirm which of those exists in said string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Actually the file is something like this:

    Code:
    15486 1 Fysics_1 Papadopoulos
    14789 3 Mathematics Nikol
    48658 2 Algebra Maria
    45888 4 C_Programing Billys
    84666 2 Physiology Lydia
    and what i want is to get each time only one "part"...and put it in another file...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	FILE *infile;
    	FILE *pfile;
    	char buf[81];
    	
    		infile = fopen("lala.txt","r");
    		pfile = fopen("text_file.txt","w");
    		if(infile == NULL){
    			printf("File does not exist.\n");
    		}
    
    		while(fgets(buf, 81, infile) != NULL){
    			fputs(buf,pfile);
    			fprintf(pfile,"\n\n");
    		}	
    		
    return 0;
    }
    Last edited by brack; 09-03-2010 at 10:34 AM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    All you're doing is reading data from the file, then writing that data out to the new file. You should look into using fscanf instead of fgets if you want to separate out parts of a formatted input file.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM