Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:04:59+00:00 2026-06-02T05:04:59+00:00

I have to write to a file as follows: A B C D …

  • 0

I have to write to a file as follows:

 A
 B
 C
 D
 ...

Each character of the alphabet needs to be written to different line in the file. I have the following program which writes characters one after another:

 FILE* fp;
 fp = fopen("file1","a+");
 int i;
 char ch= 'A';
 for(i=0; i<26; i++){
     fwrite(&ch, sizeof(char), 1, fp);
     ch++;
 }
 fclose(fp);

How should I change the above program to write each character to a new line. (I tried writing “\n” after each character, but when I view the file using VI editor or ghex tool, I see extra characters; I am looking for a way so that vi editor will show file exactly as shown above).

I tried using the following after first fwrite:

 fwrite("\n", sizeof("\n"), 1, fp);

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:05:02+00:00Added an answer on June 2, 2026 at 5:05 am
    fwrite("\n", sizeof("\n"), 1, fp);
    

    should be

    fwrite("\n", sizeof(char), 1, fp);
    

    Otherwise, you are writing an extra \0 that is part of zero-termination of your "\n" string constant (sizeof("\n") is two, not one).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following read and write file program for an Array. How
I have been trying to write a custom .screenrc file TEST as follows startup_message
I have to write an XML file using java.The contents should be written from
I have to write a biginteger into a text file using the following statement.
I have a Leiningen project.clj file as follows: (defproject insane-noises 1.0.0-SNAPSHOT :description FIXME: write
I have a file Tools.ml which contains some common utility functions I write myself.
I have to write huge data in text[csv] file. I used BufferedWriter to write
I have to write an object in to binary file.My struct looks like this.
I have to read/write an XML file that stores its data as bit masked
I have to write a series of characters in the disk file and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.