expert people please help

how to handle a file in the c++

ie like i want to create file including 10 user records & then print them too
1. Please try to come up with better topic titles than "please help". I would suggest making "how to handle a file in C++" the title.
2. Try to search the forum first. There's probably hundres of threads that explain how to use files.

http://cplusplus.com/forum/articles/1295/
Please Try to first search some information about FILE in cplusplus's tutorial, then post if you have something was not clear

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
   FILE * pFile;
   char buffer [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
     while ( ! feof (pFile) )
     {
       fgets (buffer , 100 , pFile);
       fputs (buffer , stdout);
     }
     fclose (pFile);
   }
   return 0;
}


@ropez
We posted same meaning thing during a minut
Last edited on
@SteakRider:
Your example code is 100% plain C. Altough it's possible to do the same in C++, I don't think it's good practice (you should at least use <cstdio> instead of <stdio.h>). bashi specificly asked about C++ code.

@bashi:
I would recommend looking for ifstream/ofstream classes instead of FILE.
@ropez

at first It is better to learn C, and then C++,
Don't you think it is good advice?


i am an Network under graduate & just started learning c++ i know some things about c language but not all & know all most all the basics about c++ except file handling things thats where i want your helps & i am new to this site so pardon me for the mistake i do

thankx ropez & steakRider

if you can please post a c++ file handling construct
>> at first It is better to learn C, and then C++, Don't you think it is good advice?

I don't agree that it's good advice to give C++ examples that are really C, without a comment that says it is.
closed account (z05DSL3A)
@SteakRider,

Why do you think that is is better to learn C before C++?
@Grey Wolf
Because 'C' is better than C++ for understanding C/C++ languges basic
Last edited on
closed account (z05DSL3A)
@SteakRider
My opinion (FWIW) is that you should learn C++ first; as the C part (subset) of C++ has a few advantages over C itself for the novice. These advantages include better guarantees in the form of stronger type checking, and many minor features, such as the ‘new’ operator, that are notational simpler and less error prone that there C alternatives.
Last edited on
If you learn C first, learning C++ will be more difficult, in my opinion. That is why I took advantage of C while learning C++. What I mean is that rather than including <iostream>, I chose to use <cstdio>. It greatly reduced my program's file size when compiled (statically because I was on Windows using the MinGW compiler).

The only things that I find useful from C are pointers and arrays (and structs, though you could use classes for that). Also, character arrays (C-style strings) are considerably lighter than STL strings, but they are also quite a bit more troublesome to use. I feel that it is beneficial to know how to use both, but there is the matter of /when/ to use them. Obviously, one solution for a program doesn't necessarily mean it will work for another program. Then there are the other alternatives - using a library for strings or creating your own!

As for Grey Wolf's point about the syntax being simpler, I do agree, though things like the difference between new and new[] or delete and delete[] take a bit of getting used to. As with anything, the more you use them correctly, the more natural they feel. Dynamic allocation in C++ via new and delete is certainly easier than malloc(), calloc(), realloc() and free().

I agree with ropez as well - C shouldn't be given when C++ help was requested.

And to SteakRider - C and C++ are equal in their syntax for the most part. You can learn the same things and more when you use C++. You can use C in C++, though the type checking sometimes gets in the way (like if you use the malloc(), calloc(), realloc() functions, you need to typecast the return value when using C++).

My overall point - use C when it is beneficial, but C++ is preferred. In this case, the post asked for C++. This would be using the <fstream> and headers. If you did use FILE* instead, you should change <stdlib.h> to <cstdlib> when using C++.

P.S. I apologize is some of this was difficult to read. Programming and full sentences don't seem to mix well. :P
Last edited on
My thanks to Grey Wolf and rpgfan3233 for very good posts!
Topic archived. No new replies allowed.