473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What is wrong in this program ?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *fp;
int i, n;
typedef struct student_record
{
char name[40];
int sid;
}student;

student s;
/* I presume this creates a new file if one already doesn't exist */
fp = fopen("student.dat", "ab+");
if(fp == NULL)
{
perror("file can't be opened\n!");
exit(EXIT_FAILURE);
}
else
{

printf("How many records you want to write ?\n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter record %d: student name student
id\n", i);

/* There seems to be some problem here and
its obvious during run time */
scanf("%s %d", s.name, &s.sid);
fwrite(&s, sizeof(s),1, fp);
}

}

/* doesn't print at all ? */
while(fread(&s, sizeof(s), 1, fp) == 1)
printf(" %s %d \n", s.name, &s.sid);

return 0;

}

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++

The o/p I get :

How many records you want to write ?
3
Enter record 0: student name student id
Nathan Reed 5
Enter record 1: student name student id
Enter record 2: student name student id
Apr 11 '08 #1
2 1567
On 11 Apr, 07:48, pereges <Brol...@gmail.comwrote:
fp = fopen("student.dat", "ab+");
if(fp == NULL)
{
perror("file can't be opened\n!");
exit(EXIT_FAILURE);
}
Consider the user running the program encountering
the error message:

file can't be opened
:No such file or directory

As a user, I will be really annoyed that this
message doesn't give me the pathname of the file
in question. (and slightly annoyed by the extra newline
before the ':')

Please consider writing this as:
fp = fopen( filename = "whatever", ...)
....
perror( filename );

Or, if you really feel it necessary to be
excessively verbose:

fprintf( stderr, "%s can't be opened:", filename );
perror( NULL );

(or "%s can't be opened: %s", filename, strerror( errno )
....whatever you prefer, just make sure the filename
is in the error message.)
Apr 11 '08 #2
William Pursell wrote:
[...]
Or, if you really feel it necessary to be
excessively verbose:

fprintf( stderr, "%s can't be opened:", filename );
perror( NULL );
Don't do it that way, because perror() reports the
error that errno indicates, and fprintf() might change
errno. You could wind up with

flatcat.dat can't be opened: not a typewriter

(See Question 12.24 in the FAQ.)

One way to deal with this is to save and restore errno's
value around the fprintf() call:

#include <errno.h>
...
{
int errno_save = errno;
fprintf(stderr, "%s can't be opened: ", filename);
errno = errno_save;
perror(NULL);
...

.... but in a case like this the advice to use
(or "%s can't be opened: %s", filename, strerror( errno )
.... seems better.

--
Er*********@sun.com
Apr 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
1
by: Qiangning Hong | last post by:
I decide to seperate my data collection routine from my data analysis and storage program to a seperate process, so I try to use the new subprocess model in Python 2.4. The main program spawns...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: belton180 | last post by:
CODE]../* Program function: Simulate the stack using a stack limit of 10. Display a menu for the the following. Create a stack Insert an item in the stack Pop an item from the stack ...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
12
by: broli | last post by:
#include<stdio.h> #include<stdlib.h> struct point { double x, y, z;
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.