473,378 Members | 1,394 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,378 software developers and data experts.

Program works on screen but not on disk...

I have a program below that runs fine. It accesses a datafile for
random-access transfers. Supposedly, it does this by accessing the
file in binary mode. I do not understand how it does this without
any such parameter. The lower-level open() functions use a |O_BINARY
or some such specification.
Q: Can someone tell me how I can use/modify this program and be left
with a datafile that has sensible (read: ASCII) data in it???

/* RWARRAY.C - Loads and saves an array from a database file on-disk
*/

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

#define ITEMS 7

int main()
{
char filename[85];
int count;
FILE *fileptr;
int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
int data2[ITEMS];

printf("\nEnter filename: ");
gets(filename);

/* write array to file */
if ((fileptr = fopen(filename, "w")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}

printf("Writing data items to file %s....\n", filename);

fwrite(data, sizeof(data), 1, fileptr);
fclose(fileptr);

/* Read file into array */
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}

printf("Reading data items from file....\n");
fread(&data2, sizeof(data), 1, fileptr);
fclose(fileptr);
printf("The elements of the array are: \n");
for (count = 0; count < ITEMS; count++)
{
printf("Element %d is %d\n", count, data2[count]);
}

return 0;
}
Nov 13 '05 #1
4 2794
Blankdraw <sp********@aol.com> wrote:
I have a program below that runs fine. It accesses a datafile for
random-access transfers. Supposedly, it does this by accessing the
file in binary mode. I do not understand how it does this without
any such parameter. The lower-level open() functions use a |O_BINARY
or some such specification.
Binary or non-binary mode doesn't make any difference when you use
fwrite() and fread(), it is only relevant (on some systems) when
using fprintf() and fscanf() (mostly about how the '\n' character
is interpreted).
Q: Can someone tell me how I can use/modify this program and be left
with a datafile that has sensible (read: ASCII) data in it??? /* RWARRAY.C - Loads and saves an array from a database file on-disk
*/ #include <stdio.h>
#include <string.h>
Not requred for your program.
#include <process.h>
What is process.h? That's not a standard header file and there's
nothing in your program that would make it necessary to include
the file.
#define ITEMS 7 int main()
{
char filename[85];
int count;
FILE *fileptr;
int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
int data2[ITEMS]; printf("\nEnter filename: ");
gets(filename);
Never, ever use gets(). There's no way you can make sure that the
input will fit into the buffer you pass to gets(). Use fgets()
instead.
/* write array to file */
if ((fileptr = fopen(filename, "w")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
} printf("Writing data items to file %s....\n", filename); fwrite(data, sizeof(data), 1, fileptr);
If you want to write out everything in ASCII you need the fprintf()
function, i.e.

for ( i = 0; i < ITEMS; i++ )
fprintf( fileptr, "%d\n", data[ i ] );
fclose(fileptr); /* Read file into array */
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
} printf("Reading data items from file....\n");
fread(&data2, sizeof(data), 1, fileptr);
And, if you have written out the data in ASCII, you would use fscanf():

for ( i = 0; i < ITEMS; i++ )
if ( fscanf( fileptr, "%d", data2 + i ) != 1 )
{
fprintf( stderr, "Input file is broken.\n" );
fclose( fileptr );
exit( EXIT_FAILURE );
}

(Don't forget to include <stdlib.h> should you use EXIT_FAILURE.)
fclose(fileptr);
printf("The elements of the array are: \n");
for (count = 0; count < ITEMS; count++)
{
printf("Element %d is %d\n", count, data2[count]);
} return 0;
}

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
>> I have a program below that runs fine. It accesses a datafile for
random-access transfers. Supposedly, it does this by accessing the
file in binary mode. I do not understand how it does this without
any such parameter.
The second argument of fopen() may be, for example, "r" vs. "rb" for
text vs. binary mode.
The lower-level open() functions use a |O_BINARY
or some such specification.


Binary or non-binary mode doesn't make any difference when you use
fwrite() and fread(), it is only relevant (on some systems) when
using fprintf() and fscanf() (mostly about how the '\n' character
is interpreted).


The difference between text and binary mode affects how the '\n'
character is interpreted, *REGARDLESS* of whether you use fwrite()
or fprintf().

Assuming that string points at a string (no embedded \0 characters)
that contains newline(s), and foo is a stream opened for output,
you should get the same result in the file from:

char *string;
FILE *foo;

fprintf(foo, "%s", string);
OR
fwrite(string, 1, strlen(string), foo)
What matters (if there is any difference at all: on some systems
there isn't) is how the file was opened.

Gordon L. Burditt
Nov 13 '05 #3
go***********@sneaky.lerctr.org (Gordon Burditt) wrote in
<bh********@library1.airnews.net>:

<SNIP>
The difference between text and binary mode affects how the '\n'
character is interpreted, *REGARDLESS* of whether you use fwrite()
or fprintf().

Assuming that string points at a string (no embedded \0 characters)
that contains newline(s), and foo is a stream opened for output,
you should get the same result in the file from:

char *string;
FILE *foo;

fprintf(foo, "%s", string);
OR
fwrite(string, 1, strlen(string), foo)
What matters (if there is any difference at all: on some systems
there isn't) is how the file was opened.


Question: is it good or bad practice if I fopen() text-files
in binary mode to avoid possible automatic line delimiter
translation (CR, LF, CRLF, ...) on different OSs and leave
the control over line delimiting to my program?

Irrwahn

--
If you can see it and it’s there - it’s real.
If you can’t see it, but it’s there - it’s transparent.
If you can see it but it isn’t there - it’s virtual.
If you can’t see it and it isn’t there - it’s gone.
Nov 13 '05 #4
><SNIP>
The difference between text and binary mode affects how the '\n'
character is interpreted, *REGARDLESS* of whether you use fwrite()
or fprintf().

Assuming that string points at a string (no embedded \0 characters)
that contains newline(s), and foo is a stream opened for output,
you should get the same result in the file from:

char *string;
FILE *foo;

fprintf(foo, "%s", string);
OR
fwrite(string, 1, strlen(string), foo)
What matters (if there is any difference at all: on some systems
there isn't) is how the file was opened.


Question: is it good or bad practice if I fopen() text-files
in binary mode to avoid possible automatic line delimiter
translation (CR, LF, CRLF, ...) on different OSs and leave
the control over line delimiting to my program?


If they really *ARE* text files, treat them as such. Among other
things, FTP in ASCII mode should automatically translate line
delimiters from one format to another (using a fixed standard for
traffic on the net, neither system needs to know what the OTHER
system uses). Also, you're going to have problems with your binary
so-called text files if you ever expect to manipulate them with
standard tools like a text editor or print spooler.

If you need to share the files among machines with different OSs
and line-ending conventions via, say, NFS, then maybe you should
use a portable binary format. (Portable in this sense means laying
down format rules like: NO MULTIBYTE INTEGERS (or specify the byte
ordering and size), NO FLOATING POINT (or specify the exact format
used), line endings (if you even have lines) are THIS, etc.).

Gordon L. Burditt
Nov 13 '05 #5

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

Similar topics

17
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to...
6
by: helpless newbie | last post by:
Hello. I'm a newbie in c programming. I'd like to make a program for creating an image file from a floppy disk. Is there anyone with agood point of start in doing this? Thanks.
1
by: MrBigDBaker | last post by:
Long story short. Power goes out, and comes back on. I restart my computer and everything starts ok...then the Blue screen somes up saying something is write protected or something.After...
6
by: Br | last post by:
Hi everyone, Another newbie-type question, Here's what I'd like to do: 1) Have a user type something into an input-field 2) Transfer that information into a database 3) Extract that...
2
by: Dan Tallent | last post by:
When a user launches the program, I want it to test if an update is available. You see many examples of this method to update. (Quickbooks, Norton AntiVirus, online games) I have a method...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
2
by: E C H (He of too much code) | last post by:
I am very weak in VB.NET, I am using VB.NET 2k5 Express. I have only created one program, and heck it was my first ;-) (URL below if it matters). I am trying to create a program that stays...
8
by: Jothishankar | last post by:
Hi, I am new to c#. I am trying to build an application that does backup of files to an external hard disk. My application behaves strangely. When i run the application under debug mode (F5),...
6
by: John Wright | last post by:
During my program startup I need to check for a couple of things. 1. I need to check for network availability 2. I need to check for database connectivity 3. I need to check for access to the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.