File Handling : It  is a way of performing operation on file using C language. operations are  like creation of file, writing, appending, reading and etc.

Creating File: fopen() is used to open the file and it takes two arguments. First argument is the file pointer (name of file) we want to open and other argument refers to the mode in which we want to open our file.

There are Various modes;

  1. “r” – Opens the files only for reading
  2. “w”- Creates a new file only for writting
  3. “a”- Appends or write at the end of file
  4. “r+”- Open file for reading and writing both
  5. “w+”- Creates a new files if file doesn’t exist for writing. if file exists then its contents get overwritten. it also allow us to read the file.
  6. “a+”- Enable us to read the file and at the same time allows us to append/write at the end of file.
FILE *fp; 
filename=strcat(filename,".csv"); 
fp=fopen(filename,"w+");

Write Into the File:  We can use fprintf function to write into the the file.

fprintf(fp,"Student Id, Microprocessor, RDMBMS, DCN, DS"); // used to for writing header into file
fprintf(fp,"\n%d,%d,%d,%d,%d",id,micro,rd,dcn,ds); // used to write dynamic value

Here is Full code given below for creating and writing into that file. Enjoy 🙂

#include<stdio.h>
#include<string.h>

// function for creating and writing in file
void create_marks_csv(char *filename){
FILE *fp;
int i, count, id, micro, dcn, ds, rd;

printf("\n Creating %s.csv file",filename);
filename=strcat(filename,".csv");

fp=fopen(filename,"w+");

fprintf(fp,"Student Id, Microprocessor, RDMBMS, DCN, DS");
printf("How many student's marks do you want to save?");
scanf("%d", &count);

for(i = 1; i <= count; i++){
 printf("Enter student id");
 scanf("%d", &id);
 printf("Enter Microprocessor marks");
 scanf("%d", &micro);
 printf("Enter RDBMS marks");
 scanf("%d", &rd);
 printf("Enter DCN marks");
 scanf("%d", &dcn);
 printf("Enter DS marks");
 scanf("%d", &ds);

fprintf(fp,"\n%d,%d,%d,%d,%d",id,micro,rd,dcn,ds);
}
fclose(fp);

printf("\n %sfile created",filename);
}

//main method
int main(){
 char str[100];
 clrscr();
 printf("\n Enter the filename :");

gets(str);
 create_marks_csv(str);

return 0;
}

If you want to learn more about the pointer, please do check my blog on Pointers. Hope you guys liked it. please let me know if you need any help. Thanks 🙂

127 Replies to “How to Write program for creating and writing in CSV file?”

  1. Howdy very cool blog!! Man .. Excellent .. Superb ..
    I’ll bookmark your site and take the feeds additionally?

    I am glad to seek out a lot of helpful information here within the
    submit, we want work out extra strategies on this regard, thanks for sharing.

    . . . . .

  2. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message
    home a little bit, but instead of that, this is great blog.
    A fantastic read. I will certainly be back.

  3. whoah this weblog is wonderful i really like reading your posts.
    Stay up the great work! You know, a lot of persons are looking round for this
    info, you could help them greatly.

  4. Hi there, I found your site via Google at the same time as looking for a related matter,
    your website got here up, it appears good. I’ve bookmarked it in my google bookmarks.

    Hello there, simply became alert to your blog via Google, and found that it’s truly informative.
    I’m going to be careful for brussels. I’ll appreciate for those who proceed this in future.
    Numerous other folks might be benefited from your writing.
    Cheers!

  5. That is very attention-grabbing, You are an excessively skilled
    blogger. I have joined your rss feed and stay up for searching
    for more of your great post. Additionally, I have shared your web site in my social networks

  6. You really make it appear really easy with your presentation but I in finding this matter to be actually one thing that I feel I’d never understand.
    It seems too complex and extremely extensive for me. I’m taking a look forward to your next publish,
    I will attempt to get the hang of it!

  7. You really make it seem so easy with your presentation but I find this matter to be really something that I think I would
    never understand. It seems too complex and very
    broad for me. I’m looking forward for your next post, I’ll try to get the
    hang of it!

  8. What’s Happening i am new to this, I stumbled upon this I’ve found It positively
    helpful and it has aided me out loads. I hope to contribute
    & assist different customers like its aided me. Great job.

  9. Normally I do not read article on blogs, however I would like to say that this write-up very pressured
    me to take a look at and do so! Your writing taste has been surprised me.
    Thank you, very nice post.

  10. Nice post. I was checking continuously this blog and I’m impressed!
    Extremely helpful information specially the closing phase 🙂 I handle such info a lot.
    I used to be looking for this particular info
    for a very long time. Thank you and best of luck.

  11. Terrific work! This is the type of information that
    are supposed to be shared across the net.
    Disgrace on the seek engines for no longer positioning this
    publish upper! Come on over and visit my site . Thank you =)

  12. Howdy! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading through your
    blog posts. Can you recommend any other blogs/websites/forums that cover the
    same subjects? Thanks for your time!

  13. Fantastic items from you, man. I’ve bear in mind your stuff prior to and you’re just
    extremely magnificent. I actually like what you’ve obtained right here, really like what you’re saying and the best way wherein you assert it.
    You make it entertaining and you continue to care for to keep
    it sensible. I can’t wait to read far more from you. That
    is actually a terrific website.

  14. Hey there! I just wish to give you a huge thumbs
    up for your great info you have got here on this post. I will
    be coming back to your site for more soon.

  15. Thank you, I have recently been searching for info approximately
    this subject for a long time and yours is the best I’ve found
    out so far. But, what concerning the bottom line? Are you certain about the supply?

  16. You really make it seem so easy with your presentation but
    I find this topic to be actually something which I think I
    would never understand. It seems too complex and extremely broad for me.
    I am looking forward for your next post, I’ll try to get the hang
    of it!

  17. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog?

    My website is in the very same area of interest as yours and my visitors would definitely benefit from a lot of the information you provide here.
    Please let me know if this okay with you. Regards!

    1. Hi Dannielle, I would not mind if you use it in proper manner and yes as you mentioned, you have to provide credit and source back to my website.
      Thanks and Regards!

  18. You truly make it seem fairly simple with your screen but My personal other half and i find
    such a matter to get actually something which I think We might never enjoy.
    It seems likewise complex and intensely broad appropriately.
    I am looking forward for your forthcoming post, I’ll try to get accustomed to it!

    1. Hi Andrea,
      You can tell me the issue that you are facing, i would love to help as much as i can. And again anybody who is facing problem with any topic, you can leave the comment. I will try my best to solve. Thanks!

  19. It’s really a cool and useful piece of info. I am happy that
    you shared this helpful info with us. Please stay us up to date like this.
    Thank you for sharing.

  20. I’m extremely impressed with your writing skills and also with
    the layout on your blog. Is this a paid theme or did you modify it
    yourself? Anyway keep up the nice quality writing,
    it’s rare to see a nice blog like this one these days.

  21. Hi there, I discovered your site by way of Google
    whilst searching for a similar subject, your web site got here up, it seems to be great.
    I have bookmarked it in my google bookmarks.

    Hi there, simply turned into aware of your
    weblog thru Google, and found that it’s truly informative.
    I am gonna be careful for brussels. I will appreciate for those who continue this in future.
    Many people will be benefited from your writing.
    Cheers!

  22. I’m now not positive the place you are getting your information, however great topic.
    I must spend some time studying more or understanding more.
    Thank you for excellent information I used to be in search of this info for my mission.

  23. Hey there, You have done a fantastic job.
    I’ll definitely digg it and personally suggest to my friends.
    I’m sure they’ll be benefited from this site.

  24. You truly make this kind of seem very easy with your physical appearance but My personal partner and i
    find this kind of matter to get actually a concern that I think We would never find
    out. It seems also complex and very broad i believe.
    I am looking forward for your forthcoming post, I will try to get the hang
    of it!

  25. Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter and
    said “You can hear the ocean if you put this to your ear.” She put
    the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

    1. Hi Delila, it is completely fine, and I loved to know that you and your family enjoying together, these are the moments what your daughter will remember and laugh when she gets older. So cheers and enjoy!

  26. Hello I am so delighted I found your weblog,
    I really found you by mistake, while I was researching on Aol for something else, Nonetheless I am here now and would
    just like to say thanks for a tremendous post and a all round exciting blog (I also love
    the theme/design), I don’t have time to browse it all at the moment but I have saved it and also included your
    RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic work.

  27. If some one desires to be updated with most recent technologies therefore he must be pay a quick visit this website and be up to date
    all the time.

  28. Hey I am so happy I found your weblog, I really
    found you by error, while I was looking on Askjeeve for something else, Nonetheless I am
    here now and would just like to say thanks a lot for a remarkable post and a all round exciting
    blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read more,
    Please do keep up the awesome job.

  29. I love what you guys tend to be up too. This sort of clever work and exposure!
    Keep up the excellent works guys I’ve incorporated you guys
    to my personal blogroll.

  30. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message home a little bit, but instead of
    that, this is fantastic blog. An excellent read. I
    will definitely be back.

  31. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Anyways, just wanted to say superb blog!

    1. Hi Terrie,
      Hope you are doing great! Coming to your point, Whether to show the comments or not is completely controlled by our team, we do it to avoid unwanted and unappropriated Comments. Thanks and Regards!

  32. After looking at a few of the blog posts on your web page,
    I really like your way of writing a blog. I book marked it to my bookmark website list and will be checking back in the
    near future. Take a look at my web site too and
    let me know what you think.

  33. Great goods from you, man. I’ve understand
    your stuff previous to and you are just too magnificent.
    I actually like what you have acquired here, really like what you are
    stating and the way in which you say it. You make it enjoyable
    and you still care for to keep it wise. I cant wait
    to read far more from you. This is actually a terrific website.

  34. I think that is among the most important information for me.
    And i am glad studying your article. However wanna observation on few normal
    issues, The website style is ideal, the articles is truly nice : D.
    Good process, cheers

  35. Greetings from Carolina! I’m bored at work so I decided to browse your website
    on my iphone during lunch break. I really like the information you provide here and can’t wait to take a look when I get home.
    I’m shocked at how fast your blog loaded on my phone ..

    I’m not even using WIFI, just 3G .. Anyhow, fantastic blog!

  36. Hiya very nice website!! Man .. Beautiful .. Amazing ..
    I’ll bookmark your blog and take the feeds additionally?
    I am happy to find so many helpful information right here in the
    put up, we want develop extra techniques in this regard, thanks for sharing.
    . . . . .

  37. Wow, superb blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your
    website is wonderful, let alone the content!

  38. I simply couldn’t leave your site before suggesting that I really
    enjoyed the standard info a person supply for your guests?
    Is gonna be again incessantly to check out new posts

  39. Great post. I used to be checking constantly
    this blog and I’m inspired! Very useful info specifically the closing part 🙂 I
    take care of such info a lot. I used to be looking for
    this certain information for a very long time.
    Thanks and good luck.

  40. I think this is one of the most important info for me.
    And i’m glad reading your article. But want to remark on some
    general things, The site style is wonderful, the articles is really
    nice : D. Good job, cheers

  41. What i do not understood is in reality how you are now not really much more smartly-favored
    than you may be now. You’re very intelligent. You understand thus considerably when it
    comes to this subject, made me personally believe it
    from so many numerous angles. Its like men and women aren’t interested except it’s something to accomplish with Girl gaga!

    Your own stuffs great. All the time deal with it up!

  42. Hi there! Someone in my Facebook group shared this site with us
    so I came to give it a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
    Excellent blog and excellent style and design.

  43. Just want to say your article is as astonishing.

    The clarity in your submit is just cool and that i
    can think you are knowledgeable on this subject.

    Fine along with your permission allow me to grab your feed to keep updated with forthcoming post.
    Thanks one million and please carry on the rewarding
    work.

  44. Howdy! I know this is somewhat off topic but I was wondering
    if you knew where I could find a captcha plugin for my comment form?

    I’m using the same blog platform as yours and
    I’m having difficulty finding one? Thanks a lot!

  45. An impressive share! I’ve just forwarded this onto a co-worker
    who had been doing a little homework on this.

    And he actually bought me lunch simply because I stumbled upon it for
    him… lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to discuss this topic here on your blog.

  46. Nice post. I learn something new and challenging on websites I
    stumbleupon on a daily basis. It’s always exciting to read through
    content from other writers and practice something from other
    sites.

  47. My brother recommended I might like this website. He used
    to be entirely right. This put up actually made my day.
    You cann’t imagine just how a lot time I had spent for this information! Thanks!

  48. Good post. I learn something totally new and challenging on blogs I stumbleupon everyday.
    It will always be exciting to read content from other authors and
    practice something from their sites.

  49. Attractive section of content. I just stumbled upon your weblog and in accession capital to say that I acquire actually enjoyed
    account your blog posts. Any way I will be subscribing to your augment or even I fulfillment you get admission to persistently quickly.

  50. Pretty nice post. I simply stumbled upon your blog
    and wished to say that I have really enjoyed browsing your blog posts.

    After all I will be subscribing in your feed and I am
    hoping you write again very soon!

  51. Greate pieces. Keep writing such kind of info on your blog.
    Im really impressed by it.
    Hey there, You have performed an incredible job.
    I will certainly digg it and individually suggest to my friends.
    I am sure they’ll be benefited from this site.

  52. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to design my own blog and would like
    to find out where u got this from. many thanks

    1. Hi, I’ m glad you like my website. Coming to your question yes, I have made this website by my own and the design of this site is a built in theme from WordPress site. You can also choose theme from there itself. If you need any help regarding this, you can write to me at kiranyadavde@gmail.com
      I will be happy to help you. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *