Friday, January 2, 2009

C reference 2

Some complex pointer declarations:

int *x
x is a pointer to int data type.


int *x[10]
x is an array[10] of pointer to int data type.


int *(x[10])
x is an array[10] of pointer to int data type.


int **x
x is a pointer to a pointer to an int data type – double pointers.


int (*x)[10]
x is a pointer to an array[10] of int data type.


int *funct()
funct is a function returning an integer pointer.


int (*funct)()
funct is a pointer to a function returning int data type – quite familiar constructs.


int (*(*funct())[10])()
funct is a function returning pointer to an array[10] of pointers to functions returning int.


int (*(*x[4])())[5]
x is an array[4] of pointers to functions returning pointers to array[5] of int.



* - is a pointer to

[ ] - is an array of

( ) - is a function returning

& - is an address of


File Pointers:
reading from one file and writing to another file

#include


int main(int argc, char **argv) {
int i;
char buf[100];
FILE *fin, *fout;
fin = fopen("text1.txt","r+");
fout = fopen("text2.txt","w+");
if(fin == NULL)
{
printf("Couldn't open input file \n");
exit(1);
}
if(fout == NULL)
{
printf("Couldn't open output file \n");
exit(1);
}

while (fgets(buf,100,fin) != NULL) {
fputs(buf, fout);
printf("%s", buf);
}
fclose(fin);
fclose(fout);
return 0;
}


fgets,fputs - read and write data line by line.. if we want to read / write block of data then we need code like below:

while (!feof(fin)) // feof will return 0 if end of file is reached.
{
// Reading
i = fread(buf, sizeof(char), LEN, fin);

/* read data from buf, of size of char each element, LEN number of bytes to be read, 'i' - actual number of bytes read */

buf[i*sizeof(char)] = '\0';
printf("%s", buf);

// Writing
fwrite(buf, sizeof(char), i, fout);
/* writes data from buf, of size of char each element, i number of bytes to written , returns the number of bytes actual written */

}

Random access:
fseek(), ftell() deals with random access to a file.

fseek() - will move the file position indicator the place where we need it.

int
fseek(FILE *stream, long offset, int whence);

whence =
SEEK_SET - File begining
SEEK_CUR - Current file pointer position
SEEK_END - End of file

offset - number of bytes from the "whence"

fseek() returns "0" if it is successful or non-zero if it fails.

- if SEEK_END is selected, the offset should be -ve.

We can obtain the current value of the position indicator by ftell()
long
ftell(FILE *stream);

ftell() returns the value of the current position indicator. (i.e., number of bytes from the begining to the current position indicator.

ex:
#include
#define LEN 100

void ptrSeek(FILE *fptr);
void DataRead(FILE *fptr);


int main(int argc, char **argv) {
int i;
char buf[LEN];
FILE *fin, *fout;
long offset1,offset2,offset3,offset4;

fin = fopen("text1.txt","r+");
if(fin == NULL)
{
printf("Couldn't open input file \n");
exit(1);
}
while (!feof(fin)){
offset1 = ftell(fin);
printf("Offset = %ld\n",offset1);
fgets(buf, LEN, fin);
printf("==> %s\n", buf);
}

fclose(fin);
return 0;
}

rewind() - can be used to rewind the file position indicator.
rewind(fptr); is equivalent to (void) fseek(fptr, 0L, SEEK_SET);
Example for writing and then reading from the same binary file:

#include /stdio.h/
#define LEN 100
#define MAX_NUM 5

void DataWrite(FILE *fptr);
void DataRead(FILE *fptr);


int main(int argc, char **argv) {
int i;
char buf[LEN];
FILE *fin, *fout;
long offset1,offset2,offset3,offset4;

fin = fopen("text1.bin","wb+");
if(fin == NULL)
{
printf("Couldn't open input file \n");
exit(1);
}

DataWrite(fin);
rewind(fin);
DataRead(fin);

fclose(fin);
return 0;
}

void DataWrite(FILE *fout)
{
int i;
double buf[MAX_NUM] = {123.44, 345.53, 134.12, 865.45, 454.34};
for (i=0; i/MAX_NUM; i++)
{
printf("%5.2f\n",buf[i]);
fwrite(&buf[i], sizeof(double), 1, fout);
}
}

void DataRead(FILE *fin)
{
int i;
double x;
for (i=0; i/MAX_NUM; i++)
{
fread(&x,sizeof(double),(size_t)1, fin);
printf("%5.2f\n",x);
}
}

fscanf() and fprintf()
allows the programmer to specify the I/O streams.

ex:
fscanf(fileptrIn, "%i", &value);
fprintf(fileptrOut, "Average of %i numbers = %f \n", count, total/(double)count);

if successful fscanf() returns the number of data items read. If fails, it returns EOF.
fprintf() returns the number of formatted expressions, else it returns -ve value.

remove(filename) - Delete the file with name "filename"
rename(oldname, newname) - Rename a file
rmdir() - Removes a directory.
mkdir() - Creates a directory

Pre-processor directives:
#error - prints the error message including the tokens specified

ex:

#if (MyVAL != 2 && MyVAL != 3)

#error MyVAL must be defined to either 2 or 3

#endif


#pragma - ??

# - argument to marcro
causes a replacement text token to be converted to a string surrounded by double quotes

ex:

#define
HELLO(x) printf("Hello, " #x "\n");
causes Hello(John) expanded to printf("Hello, " "John" "\n");

## - concatenate two arguments.

#define CAT(p, q) p ## q .

CAT(O,K) replaced as OK in the program.

#line - preprocessor directive causes the subsequent source code lines to be renumbered starting with the specified constant integer value. The directive:
#line 100
- Starts line numbering from 100 beginning with the next source code line.

Pre defined macros:

__DATE__ - The date the source file is compiled (a string of the form "mmm dd yyyy" such as "Jan 19 1999").

__LINE__ - The line number of the current source code line (an integer constant).

__FILE__ - The presumed names of the source file (a string).

__TIME__ - The time the source file is compiled (a string literal of the form :hh:mm:ss).

__STDC__ - The integer constant 1. This is intended to indicate that the implementation is ANSI C compliant.

No comments: