3

I want to write some data into a file line by line.

   int main ()
   {
        char* mystring = "joe";
        int i  ;
        FILE * pFile;
        pFile = fopen ("myfile.txt", "wb");
        for(i = 0 ; i <  10 ; i++)
        {
            fprintf(pFile,"%s\n",mystring);
        }
        fclose (pFile);
        return 0;
  }

I am using new line especial charater so that new data will go into next line.

Problem is at last line i dont want newline.

Note: Just for the demo i use for loop. In real situation I used linked list to iterate the data hence I don't the length.

Please tell me how to remove last line from file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Has anyone noticed how user2684719 has now asked 7 questions and gotten a lot of help from people, yet he/she hasn't accepted a single answer? And how all of his/her comments express no thanks whatsoever to people who've spent their own time trying to help him/her? – phonetagger Aug 28 '13 at 14:38
  • ...now make that 8 questions: http://stackoverflow.com/questions/18502257/array-subscript-has-type-char-wchar-subscripts – phonetagger Aug 29 '13 at 16:19
  • ...and now 9 questions: http://stackoverflow.com/questions/18528411/how-get-rid-of-warning-deprecated-conversion-from-string-constant-to-char – phonetagger Aug 30 '13 at 13:56

6 Answers6

8

There are a couple simple answers:

A. Truncate the file by one newline character when you get to the end of your list.

B. Print the newline before the string, but only if not the first line:

if (i > 0)
    fputs("\n", pFile);
fputs(mystring, pFile);

Note that this doesn't rely on having a for loop; it just requires that i only be 0 for the first line.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Truncating the file is error-prone because a newline character is two bytes on some operating systems but only one byte on others. – Ian Goldby Aug 28 '13 at 07:26
  • @Ian: I think that since the OP is using `"wb"` as the mode, the newline will be written as a single character. – Gabe Aug 28 '13 at 15:42
  • It's a little odd to use a counter if there's no loop. Perhaps a `bool` that expresses the condition would be a clearer choice? Also, your code doesn't ever initialize or increment the counter. – user1118321 Aug 29 '13 at 01:11
  • @user1118321: Feel free to declare `i` as a `bool`, or even use a different variable name. The code will work the same either way. – Gabe Aug 29 '13 at 02:12
4

I think something like this would be good:

fprintf (pFile, "%s%s", (i > 0 ? "\n" : ""), mystring);
johnish
  • 390
  • 5
  • 13
  • @user814064 It's the newline following the previous string. If you printed the newline after the string you'd end the file with a newline (and the first two strings would be on the same line). – Ian Goldby Aug 28 '13 at 07:27
1

An easy way would be to break the printing of the string and the printing of the newline into separate statements, then conditionalize the printing of the newline. Something like this;

for (i = 0; i < 10; i++)
{
    fprintf(pFile, "%s", mystring);
    if (i < 9) // Or whatever condition you need - could be "atEndOfList()" or whatever.
        fprintf (pFile, "\n");
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • The OP states that in real life, they don't know the length of their list. Hence, they can't just check `i < 9` like you propose. – Gabe Aug 28 '13 at 04:54
  • 1
    Sure, but there will be some condition. It may be that they're at the tail of their list. Whatever condition is necessary. I was just explaining in the terms he used in his example. – user1118321 Aug 28 '13 at 04:55
  • The loop is running backward so you'll be printing a newLine at the end. – dcaswell Aug 28 '13 at 06:06
  • The whole reason they asked the question is that when they're printing a line, they don't yet know if there are any lines to follow. – Gabe Aug 28 '13 at 15:45
  • @Gabe where does the poster say that? I don't see anything saying that. They even say they'll be using a linked list. They may not know the length of the list, but they still know the last item because either it is equal to "tail of list" or it's "next" pointer is NULL, or something like that. – user1118321 Aug 29 '13 at 01:09
0

Since you are using a linked list, you can check if the current item has a next item. If it has, print a newline and if it doesn't, don't print a newline since it is the last item.

erelender
  • 6,175
  • 32
  • 49
0

In the following code snippet, 10 can be changed to any value without having to modify any other tests or ifs.

    pFile = fopen ("myfile.txt", "wb");

    fprintf(pFile, "%s", mystring);
    for(i = 1 ; i <  10 ; i++)
    {
        fprintf(pFile, "\n");
        fprintf(pFile, "%s", mystring);
    }
    fclose (pFile);
JackCColeman
  • 3,777
  • 1
  • 15
  • 21
0

You dont know the length, but there must be a condition which when becomes false, program will stop putting string into file, so inside the loop, write if condition which is same as terminating condition for your loop. for eg, for your above prog, it should be :

for(i=0;i<10;i++)
{ 
    if((i+1)!<10)    //executed when i=9
    fprintf(pFile,"%s",mystring);
    else             //executed for i=0 to 8
    fprintf(pFile,"%s\n",mystring);
}
Sumedh
  • 404
  • 3
  • 11