Newline writing to a file

This is a discussion on Newline writing to a file within the C Programming forums, part of the General Programming Boards category; If I am writing data to a .txt file, how can I get it to append to a newline instead ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    5

    Newline writing to a file

    If I am writing data to a .txt file, how can I get it to append to a newline instead of append to the first line?

    Thanks in advance!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Your question is a bit odd.

    Well if you're using fprintf(), then by adding this line before you write the data:
    fprintf(file, "\n");
    you ensure that the data you write will be on a new line.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    5
    See that is what I thought too, but that doesn't work, it still just appends to the same line in the file.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Perhaps the editor you're using to view the file is fooling you.

    Have you opened the file in append mode?

    Otherwise post some code.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    5
    fp = fopen(DATAFILE, "a+");

    fprintf(fp, "\n");
    fprintf(fp, "Emp# ");
    fprintf(fp, "%d", emp.eNum);
    fprintf(fp, " ");
    fprintf(fp, "| Name: ");
    fprintf(fp, emp.fName);
    fprintf(fp, " ");
    fprintf(fp, emp.lName);
    fprintf(fp, " ");
    fprintf(fp, "| Hire Date: ");
    fprintf(fp, emp.hDate);
    fprintf(fp, " | ");
    fprintf(fp, newstring); // DEPARTMENT: added with strcopy()
    fprintf(fp, " ");
    fprintf(fp, "| Salary: ");
    fprintf(fp, "%2.2f", annualsalary);
    //fprintf(fp, "\n");

    fclose(fp);

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    5
    Actually it could be because I am looking at the file in notepad, because when I open it in word, it opens on different lines

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    5
    Alright, is there a way I can write to a text file, so it is actually on a newline when I open it in textpad? instead of just have the newline character appended to the first line? Because I need to read in the file line by line and display it on to the screen line by line....


    Thoughts?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Well if you move the newline from the beginning to the end, then it should all work just nicely
    Code:
    fp = fopen(DATAFILE, "a+");
    
    // fprintf(fp, "\n");  // not here
    fprintf(fp, "Emp# ");
    fprintf(fp, "%d", emp.eNum);
    fprintf(fp, " ");
    fprintf(fp, "| Name: ");
    fprintf(fp, emp.fName);
    fprintf(fp, " ");
    fprintf(fp, emp.lName);
    fprintf(fp, " ");
    fprintf(fp, "| Hire Date: ");
    fprintf(fp, emp.hDate);
    fprintf(fp, " | ");
    fprintf(fp, newstring); // DEPARTMENT: added with strcopy()
    fprintf(fp, " ");
    fprintf(fp, "| Salary: ");
    fprintf(fp, "%2.2f", annualsalary);
    fprintf(fp, "\n");  // newline here
    
    fclose(fp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21