meditationatae

Just another WordPress.com site

Simple C test programs that open and read a file

To open a file for reading in the C programming language, one can pass to fopen a string containing the absolute path to the file. It looks like this:

fopen(“/home/david/fname”, “r”)

string = “/home/david/fname” = path to file, and

“r” for open in read-only mode.

Because it’s a statement, it must end with a `;’ like this:

fopen(“/home/david/fname”, “r”);

I guess the fopen function returns a file pointer …

In any case, the complete line of code is this:

in = fopen(“/home/david/fname”, “r”);

where in the inital declarations one has:

FILE *in;

My question was: what happens if instead we write:

in = fopen(“/home/david/fname “, “r”);

with fname followed by a space?

More on that later.

In my home directory /home/david/,

I redirected the output of “cal 2017” to fname like this:

$ cal 2017 > fname  [ENTER]

Then, fname has 1998 characters in Linux CentOS 6.8, including the digits for the year 2017.

 

My test program was to count each kind of digit, and show the total by digit, as well as the grand total of digits.

Executing the compiled program and the output went like this:

$ ./testprogram01.out
The file has in total:
36 0s
164 1s
156 2s
54 3s
36 4s
36 5s
36 6s
37 7s
36 8s
35 9s

The file has in total: 626 digits.

 

So according to this program, using the bash shell, typing

“cal 2017” produces a calendar for 2017, with 626 digits in all.

 

The source code is copied now:

#include <stdio.h>
int main(void)
{
int j;
unsigned char car;
int num0 = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
int num7 = 0;
int num8 = 0;
int num9 = 0;
int digits = 0;
FILE *in;

in = fopen(“/home/david/fname”, “r”);

for(j = 0; j < 1998; j++)
{
fscanf(in, “%c”, &car);

if(car == ‘0’)
{
num0++;
}

if(car == ‘1’)
{
num1++;
}

if(car == ‘2’)
{
num2++;
}

if(car == ‘3’)
{
num3++;
}

if(car == ‘4’)
{
num4++;
}

if(car == ‘5’)
{
num5++;
}

if(car == ‘6’)
{
num6++;
}

if(car == ‘7’)
{
num7++;
}

if(car == ‘8’)
{
num8++;
}

if(car == ‘9’)
{
num9++;
}
}

fclose(in);

printf(“The file has in total:\n”);
printf(“%d 0s\n”, num0);
printf(“%d 1s\n”, num1);
printf(“%d 2s\n”, num2);
printf(“%d 3s\n”, num3);
printf(“%d 4s\n”, num4);
printf(“%d 5s\n”, num5);
printf(“%d 6s\n”, num6);
printf(“%d 7s\n”, num7);
printf(“%d 8s\n”, num8);
printf(“%d 9s\n”, num9);

digits = num0 + num1 + num2 + num3 + num4 + num5;
digits = digits + num6 + num7 + num8 + num9;

printf(“\n”);
printf(“The file has in total: %d digits.\n”, digits);

return 0;
}

=======

Next, we edit testprogram01.c and add a space after fname in the string: “/home/david/fname” , saving the result to testprogram02.c :

The  relevant part of the 2nd program is this:

#include <stdio.h>
int main(void)
{
int j;
unsigned char car;
int num0 = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int num6 = 0;
int num7 = 0;
int num8 = 0;
int num9 = 0;
int digits = 0;
FILE *in;

in = fopen(“/home/david/fname “, “r”);

 

(The SPACE after “fname is apparent…).

Next, compile testprogram02.c:

gcc -o testprogram02.out testprogram02.c

Compilation successful, executable is named: testprogram02.out.

Now, we try to run testprogram02.out :

$ ./testprogram02.out [ENTER]
Segmentation fault (core dumped)

It seems therefore that

“/home/david/fname ” (with a space after the ‘e’ in fname)

does not work as a substitute for the proper string:

“/home/david/fname”  .

 

Advertisements

Written by meditationatae

February 19, 2017 at 2:50 pm

Posted in History

%d bloggers like this: