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?
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 ...
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.
fputs doesn't read, it writes. Are you getting mixed up between fputs and fgets?
oh sorry for my mistake...i mean writes...thanks for correcting me...
do you know why?
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.
I used to be an adventurer like you... then I took an arrow to the knee.
Actually the file is something like this:
15486 1 Fysics_1 Papadopoulos
14789 3 Mathematics Nikol
48658 2 Algebra Maria
45888 4 C_Programing Billys
84666 2 Physiology Lydia
#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.
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.
Recent additions
Similar Threads
Line Counting
airport Log program using 3D linked List : problem reading from file
Going out of scope
God