|
From: Johannes P. <joh...@pr...> - 2001-09-04 05:37:06
|
Okay I dont reaaly know what You want to do. Considering you want to read
in a structure I will say use fread !!! else try something like
this if you want a block of data
//SNIP
#include <stdio.h>
#include <fcntl.h>
#include <alloc.h> /* alloc.h for Borland compilers,
malloc.h for Microsoft compilers */
#define BUFFER_SIZE 0x8000 /* 32Kb data buffer */
main(int argc, char *argv[]) {
int Handle;
unsigned int Checksum;
unsigned char *WorkingBuffer;
int WorkingLength;
if ( argc != 2 ) {
printf("usage: checksum filename\n");
exit(1);
}
if ( (Handle = open(argv[1], O_RDONLY | O_BINARY)) == -1 ) {
printf("Can't open file: %s\n", argv[1]);
exit(1);
}
/* Get memory in which to buffer the data */
if ( (WorkingBuffer = (unsigned char *)malloc(BUFFER_SIZE)) == NULL ) {
printf("Can't get enough memory\n");
exit(1);
}
/* Initialize the checksum accumulator */
Checksum = 0;
/* Process the file in 32Kb chunks */
do {
if ( (WorkingLength = read(Handle, WorkingBuffer,
BUFFER_SIZE)) == -1 ) {
printf("Error reading file %s\n", argv[1]);
exit(1);
}
/* Checksum this chunk if there's anything in it */
if ( WorkingLength ) {
//Beneath part is in assembler file. So I commented it out
//ChecksumChunk(WorkingBuffer, WorkingLength, &Checksum);
}
} while ( WorkingLength );
/* Report the result */
printf("The checksum is: %u\n", Checksum);
exit(0);
}
//END SNIP
NOTE : The above code will always have a checksum of 0 as it is not calling
the assembler function that is in a different file.
else a more simple straight forward approach will be
//SNIP
/*
* Program to calculate the 16-bit checksum of the stream of bytes
* from the specified file. Obtains the bytes one at a time via
* getc(), allowing C to perform data buffering.
*/
#include <stdio.h>
main(int argc, char *argv[]) {
FILE *CheckFile;
int Byte;
unsigned int Checksum;
if ( argc != 2 ) {
printf("usage: checksum filename\n");
exit(1);
}
if ( (CheckFile = fopen(argv[1], "rb")) == NULL ) {
printf("Can't open file: %s\n", argv[1]);
exit(1);
}
/* Initialize the checksum accumulator */
Checksum = 0;
/* Add each byte in turn into the checksum accumulator */
while ( (Byte = getc(CheckFile)) != EOF ) {
Checksum += (unsigned int) Byte;
}
/* Report the result */
printf("The checksum is: %u\n", Checksum);
exit(0);
}
//END SNIP
Now this is basic stuff that I can think of right now. Futher more if you
can give more information on what you want to achieve maybe we can help
hope this helps
Johannes
At 21:34 03/09/2001 +0100, you wrote:
>Hi,
>
>I havn't programmed in C for a couple of years now, I've been concentrating
>on C++.
>I have forgotten a few things so I'm watching this group for input, and I've
>been impressed by what I have seen.
>
>I had a look at your code and spotted a few things:
>
>1) For fopen() use "w+" as you need to read it as well.
>2) Use ' fseek (fhandle, 0L, SEEK_SET);' to reposition the file to the
>beginning for reading.
>3) Dereference fhandle (*fhandle) in the fprintf() statement and this will
>read in the string.
>3) fprintf() and fscanf() do not move the file position and I can't remember
>how to do this. So another call to fprintf() will not read the stringified
>number on the next line. I thought I could use:
> 'fseek (fhandle, len, SEEK_CUR);' where len is the returned value
>from a successful call to fprintf()
> However, this doesn't work. Instead I prefer to use:
>
> int c;
> while ((c = fgetc (fhandle)) != EOF)
> {
> fputc (c, stdout);
> }
>
>Any comments?
>
>Hope this helps.
>
>Michael
>
>----- Original Message -----
>From: Johannes Pretorius <joh...@pr...>
>To: <jsc...@gm...>
>Cc: <dev...@li...>
>Sent: Monday, September 03, 2001 2:07 PM
>Subject: Re: [Dev-C++] how to use strings
>
>
> > this here is a simple example that will make more sense. For considering
> > that you are trying to mix
> > your WINAPI functions and write to the handle of the file allocators. I do
> > not think that it is possible in
> > the compiler version. Maybe with a Microsft compiler.
> >
> > else this is a VERY simple way to do it that works. I hope this help.,
> > Please mail if I misunderstand you.
> >
> > file://====== SNIP =========
> >
> > #include <iostream.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> >
> >
> > struct test{
> > char data [200];
> > }testdata;
> >
> > void main(void){
> > char outstring[200];
> > FILE *fhandle;
> > int actualnumber;
> >
> > file://open in write mode (override if exist);
> > fhandle = fopen("c:\\medcelldata.txt","w");
> > /*CreateFile("c:/medcelldata.txt",
> > GENERIC_READ | GENERIC_WRITE,
> > 0,
> > NULL,
> > OPEN_ALWAYS,
> > FILE_ATTRIBUTE_NORMAL,
> > NULL
> > );
> > */
> >
> > if(fhandle == NULL)
> > {
> > printf("\nNO File present");
> > }
> >
> > file://copy test to structuer
> > strcpy(testdata.data,"Hallo World\n");
> >
> > actualnumber = 1000;
> >
> > file://write using fwrite (BETTER for Structures)
> > fwrite(&testdata, sizeof(testdata), 1, fhandle);
> >
> > file://convert integer
> > itoa(actualnumber, outstring, 10);
> >
> > file://write with fprintf to file
> > fprintf(fhandle,"OutString : %s",outstring);
> >
> > file://clsoe file
> > fclose(fhandle);
> > }
> >
> >
> >
> >
> > file://========= END SNIP =========
> >
> > Hope it helps
> > Johannes
> >
> >
> >
> > At 14:03 03/09/2001 +0200, you wrote:
> > >On 3 Sep 2001, at 11:57, Johannes Pretorius wrote:
> > >
> > > > Johannes Pretorius <joh...@pr...>
> > >
> > >
> > >Here it is ! i allready change something to make it run - but in the
> > >written file there only occurs 1- or two figures !
> > >
> > >How can i use the fprintf - function to access the file ????
> > >
> > >
> > >
> > >#include <iostream.h>
> > >#include <stdio.h>
> > >#include <stdlib.h>
> > >#include <windows.h>
> > >
> > >// vars
> > >
> > >HANDLE hcom; // file handler
> > >HANDLE hfile;
> > >DWORD dwWritten;
> > >
> > >int value[8];
> > >int numofdata[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
> > >
> > >BYTE noise; // for testing only
> > >BYTE activeitem; // no of active row
> > >
> > >BYTE in_buffer[35]; // input buffer = 8
>needed
> > >BYTE out_buffer[35]; // output buffer
> > >
> > >int writepoint;
> > >int readpoint;
> > >int writepointin; // write pos inbuffer
> > >int readpointin; // read pos inbuffer
> > >int goodvalue;
> > >int tempvalue;
> > >int actualnumber;
> > >
> > >BYTE input;
> > >BYTE count;
> > >int globalschleife;
> > >LPTSTR lpMessage;
> > >
> > >int progmode; // bin or asc
> > >
> > >
> > >....
> > >
> > > hfile = CreateFile("E:/medcelldata.txt",
> > > GENERIC_READ | GENERIC_WRITE,
> > > 0,
> > > NULL,
> > > OPEN_ALWAYS,
> > > FILE_ATTRIBUTE_NORMAL,
> > > NULL
> > > );
> > >
> > > if(hfile == INVALID_HANDLE_VALUE)
> > > {
> > > printf("\nNO File present");
> > > }
> > >
> > > pstr = "MedCellCom 1.2\n\n ";
> > > WriteFile(hfile, pstr, 16, &dwWritten, NULL);
> > >
> > >
> > > pstr = "Prozess - Protokoll vom : \n ";
> > >
> > > WriteFile(hfile, pstr, 27, &dwWritten, NULL);
> > >
> > >
> > > itoa(actualnumber, outstring, 10);
> > >
> > > WriteFile(hfile, outstring, 10, &dwWritten, NULL);
> > >
> > > WriteFile(hfile, "456", 3, &dwWritten, NULL);
> > >
> > > printf("%d\t", dwWritten);
> > > printf("%s\n", outstring);
> > >
> > >
> > >
> > >The output shows only
> > >
> > > MedCellCom 1.2
> > >
> > > Prozess - Protokoll vom :
> > >2
> > >
> > >Where "2" ist the actualnumber
> > >
> > >I do not see the 456 test string allthoug the screen
> > >shows the correct outstring and 3 chars written !
> > >
> > >It is a big loop, where many lines are written and
> > >shown on the screeen it works and the prog terminates.
> > >
> > >When writing only dummy strings like explicit
> > >"iugnuztf" it works and show 200 lines with exactly
> > >this text !
> > >
> > >????
> >
> >
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
|