|
-
Mar 18th, 2002, 08:20 AM
#1
Thread Starter
New Member
How Can I write strings in a new line with C Ansi ?e
I wanna write string in a new line. How can i do that ?
int main(int argc, char* argv[])
{
FILE *ponteiro_arquivo;
int i =0;
strcpy(sLinha, "0001 123456 nf 0,12\n"); /* copy 1 line*/
for (i =0 ; i < 5; i++)
if((ponteiro_arquivo = fopen ("c:\\gravar.txt", "w"))!= NULL){
fprintf(ponteiro_arquivo, sLinha);
/*always writing only the last line */
fclose (ponteiro_arquivo);
}
Thanks for help me
-
Mar 18th, 2002, 11:32 AM
#2
Member
You are overwriting the file in each iteration of the loop. Try moving the open and close statements outside of your loop.
Or you can open the file for "write/append", in which case your printf will simply add a line at the end of the file. In your example, both of these methods would produce the same output.
Hope this helps,
Cedric
-
Mar 18th, 2002, 12:40 PM
#3
Thread Starter
New Member
iT'S RUN
THANKS FOR YOUR HELP... IT'S RUN NOW.
I JUST OPEN THE FILE WITH OPTION "a".
-
Mar 18th, 2002, 04:52 PM
#4
it's still bad because it leaves with a lot of opened files hanging around - VERY bad! And it causes you to reopen it often - VERY slow!
Code:
int main(int argc, char* argv[])
{
FILE *ponteiro_arquivo;
int i =0;
strcpy(sLinha, "0001 123456 nf 0,12\n"); /* copy 1 line*/
if((ponteiro_arquivo = fopen ("c:\\gravar.txt", "wt"))== NULL)
{
printf("Error!\n");
return -1;
}
for (i =0 ; i < 5; i++)
{
fprintf(ponteiro_arquivo, sLinha);
/*always writing only the last line */
}
fclose (ponteiro_arquivo);
// ...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|