Why am I getting "Segmentation Fault" when I run my program? - c

My program decodes an image that is covered by random pixels, to decode the image, I have to multiply each pixel's red color component by 10. The green and blue color components are the same values as the new red component. I've created multiple helper functions, to make the code easier to read in main, but when I try to run my a.out, I keep getting "Segmentation Fault". I can't seem to find my mistakes! Help is appreciated.
void check_argument(int arg_list)
{
if (arg_list < 2)
{
perror("usage: a.out <input file>\n");
}
}
void print_pixel(int a, FILE *out)
{
int r, g, b;
r = a * 10;
if (r > 255)
{
r = 255;
}
g = r;
b = r;
fprintf(out, "%d\n", r);
fprintf(out, "%d\n", g);
fprintf(out, "%d\n", b);
}
void read_header(FILE *in)
{
char str[20];
for (int i = 0; i < 3; i++)
{
fgets(str, 20, in);
}
}
FILE* open_files(FILE *infile, char *input[])
{
infile = fopen(input[1], "r");
if (infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return infile;
}
void decode(int arg_list, char *in[])
{
FILE *input, *output;
int check, red, green, blue;
open_files(input, in);
output = fopen("hidden.ppm", "w");
fprintf(output, "P3\n");
fprintf(output, "%d %d\n", 500, 375);
fprintf(output, "255\n");
read_header(input);
check = fscanf(input, "%d %d %d", &red, &green, &blue);
while (check != EOF)
{
print_pixel(red, output);
check = fscanf(input, "%d %d %d", &red, &green, &blue);
}
fclose(input);
fclose(output);
}
int main(int argc, char *argv[])
{
check_argument(argc);
decode(argc, argv);
}

After calling open_files(input, in); you will not have the file handle in input.

As this is supposed to be homework, I'll try to show you the some common source of bugs and how to find them.
Variables which are used must (should) be assigned to before that. This counts especially for pointers, e. g. FILE *.
If a function (e. g. fopen()) fails, it normally indicates this by returning a special value which must be checked before continuing.
To check which value a variable has, you can use printf() to show it.
This is for finding principal errors such as segfaults.
But logical errors are hard to find as well: if you read 3 values and store them into variables, it might be more useful to use them all instead of only one of them. (But maybe this one is not yet the goal of this exercise.)
I wrote the lines before this before I learned that it is not the task to search for bugs in a given program, but to write a program by yourself, so I'll get a little more concrete by now.
A FILE * is something returned by fopen(). You can return it or you can write it to a variable or another memory location indirectly pointed to by a pointer "one level deeper".
So you should rewrite your open_files() (BTW: why file*s*? It's currently only one...):
either for returning the value (preferrable):
FILE* open_files(char *input[])
{
FILE *infile = fopen(input[1], "r");
if (infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return infile;
}
and call it with
input = open_files(input);
or with "pass by reference":
void open_files(FILE **infile, char *input[])
{
*infile = fopen(input[1], "r");
if (*infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return *infile;
}
and call it with
open_files(&input, in);
Only doing that you'll have your variable input at the caller's site really written to.

Related

How to dump txt file in C?

#include <stdio.h>
int main(void) {
int name;
int arrival_time;
int size;
int ret;
FILE * fp = fopen_s("C:\\NIA\\data.txt", "rt");
while (1)
{
ret = fscanf_s(fp, "%d %d %d", &name, &arrival_time, &size);
if (ret == EOF)
break;
printf("%d %d %d \n", name, arrival_time, size);
}
return 0;
}
I want to dump my txt file to project but errors are coming out. I'm confused about memory initiation and file format, variables, etc. How can I fix this and print values well?
My txt file is :
Your question lacks the most important information: What is going wrong.
When I compile your code, I get errors for fopen_s. (OK, this is mainly because I use gcc ;) )
The manual tells us how this function looks like:
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
This means, you must use it like this:
errno_t err;
FILE *fp;
err = fopen_s(&fp, "C:\\NIA\\data.txt", "rt");
if (err != 0)
{
fprintf(stderr, "The file was not opened\n" );
exit(1);
}
Or you stick to standard functions and use them as you already tried:
FILE *fp;
fp = fopen("C:\\NIA\\data.txt", "rt");
if (fp = NULL)
{
fprintf(stderr, "The file was not opened\n" );
exit(1);
}
You should definitely add checks for all return values. At least for I/O related functions like fopen and scanf.
Also closing your file would be adviseable. While it is only opened in read mode, it will not cause much trouble as it is closed automatically on program termination, but it is surely good style to do it.
An improved version could look like this:
(As you do not scan strings, there is no benefit using MS non-standard function scanf_s)
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int name;
int arrival_time;
int size;
FILE * fp = fopen("data.txt", "rt");
if (fp == NULL) {
perror("File data.txt cannot be opened");
exit(1);
}
while (fscanf(fp, "%d %d %d", &name, &arrival_time, &size) == 3)
{
printf("%d %d %d\n", name, arrival_time, size);
}
fclose(fp);
return 0;
}
This prints the content of your data.txt file on the console.
If dumping you txt file means closing the txt file after using it, you can use the following
fclose(fp);
before the return 0;

Read Values from a file and store it in 2D matrix

I am trying to read values from a file and after some operation write to another file. Here facing a slight issue as I am also trying to save values in a 2D Array and displaying it. My file read and file write are showing correct results but my program throws an exception when it comes to display matrix part.
#include <stdio.h>
#include <ctype.h>
#ifndef NULL
#define NULL ((void *) 0)
#endif
int main(void)
{
FILE *file = NULL; //for file read
FILE *fptr = NULL; //for file write
int mat[182][274];
// code to read and display number from file
// open file for reading
file = fopen("file.txt", "r");
fptr = fopen("file1.txt", "w");
int i = 0,j=0;
fscanf (file, "%d", &i);
while (!feof (file))
{
symbol = fgetc(file);
if (symbol == '\n' || feof(file))
{
fprintf (fptr,"\n");
printf("\n");
}
else{
j=255-i;
mat[i][j]=j;
fprintf (fptr,"%d ", j);
fprintf (fptr," ");
printf ("%d ", j);
}
fscanf (file, "%d", &i);
}
fclose (file);
fclose (fptr);
//Facing issue in this part
int k;
int l;
for (k=0;k<=182;k++)
{
for(l=0;l<=274;l++)
{
printf("%d ", mat[k][l]);
}
}
return 0;
}
Arrays in C start at 0 and end at (array_size - 1).
As you're accessing memory just outside the array, you're most likely experiencing segmentation faults.
To fix this issue, change these lines:
for (k=0;k<182;k++)
{
for(l=0;l<274;l++)
{
printf("%d ", mat[k][l]);
}
}
Notice that I changed the relational operators from <= and >= to < and >, respectively.
Along with that, you may need to fully initialize your array. Odd values may be printed if the array is not initialized. (#Weather Vane).
However, to best be sure if this is the case, we need file.txt and file1.txt.

Debug Assertion Error in C

got some code here that won't compile correctly because it is saying that my pointer is already null when i am testing for a not null expression in my main function. here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCODE 53
#define MAXMESSAGE 256
void getCode(char *codeIn, char *filename) {
FILE *codeFile;
/* Open the file with the code */
codeFile = fopen(filename, "r");
if (codeFile == NULL) {
printf("Error opening the code file - program terminated\n");
exit(1);
}
/* Read the first (and assumed only) line from the file */
fgets(codeIn, MAXCODE, codeFile);
/* Terminate the string with /0 */
codeIn[MAXCODE] = '\0';
/* Close the file */
fclose(codeFile);
return;
}
int getMessage(int *message, char *filename) {
FILE *messageFile;
int counter = 0;
/* Open the file with the message */
messageFile = fopen(filename, "r");
if (messageFile == NULL) {
printf("Error opening the message file - program terminated\n");
exit(1);
}
/* Read one number at a time from the file and store it */
while (!feof (messageFile))
{
fscanf (messageFile, "%d", (message+counter));
counter++;
}
/* Close the file */
fclose(messageFile);
return (counter);
}
void sortMessage(int *message, int size) {
int i, j, temp;
for (i=0; i<size-1; i++) {
for (j=i; j<size; j++) {
if (message[i]>message[j]) {
temp = message[i];
message[i] = message[j];
message[j] = temp;
}
}
}
return;
}
void decodeMessage(char *codeIn, int *message, int size) {
FILE *outputFile;
int i = 0;
/* Open the output file */
outputFile = fopen("csis.txt", "w");
if (outputFile == NULL) {
printf("Error opening the output file - program terminated\n");
exit(1);
}
for (i=0; i< size; i++) {
fprintf(outputFile, "%c", codeIn[message[i]%100]);
printf("%c", codeIn[message[i]%100]);
}
printf("\n");
/* Close the file */
fclose(outputFile);
return;
}
int main(int argc, char *argv[])
{
char code[MAXCODE];
int msg[MAXMESSAGE];
int msgSize;
if (argc != 3) {
printf("This program takes two arguments: the name of the file with the code, and the name of the file with the encoded message\n");
}
getCode(code, argv[1]);
msgSize = getMessage(msg, argv[2]);
sortMessage(msg, msgSize);
decodeMessage(code, msg, msgSize);
return;
}
So basically my code is using two files called codefile.txt and msgfile.txt to decode the secret message and write the decoded sequence to a new text file called csis.
As woolstar pointed out in the comments, you don't need to NUL terminate your codeIn array following fgets, because fgets will do that for you. In fact, this constitutes an overflow which we can best see by considering what happens when MAXCODE is 1: codeIn contains only one element: codeIn[0], and accessing codeIn[1] is an error.
Similarly, since MAXCODE is 53 and that's how many elements pointed to by codeIn, codeIn[message[i]%100] is suspicious because there's a potential for message[i]%100 to be an invalid index. While we're on this note, it might be wise to make message[i] an unsigned int so that it can't be negative. The format specifier (for printf and scanf) corresponding to unsigned int is %u.
while ( !feof(messageFile) ) is wrong because the EOF flag isn't set until an attempt is made at reading. Between attempting to read and your EOF test, however, you've incremented counter which means you've counted one too many items. Perhaps your loop should look like this:
while (fscanf(messageFile, "%d", (message+counter)) == 1)
{
counter++;
}
Note that this code assumes you've chosen to keep message[i] as an int. If you've chosen to use unsigned int instead, of course you'll want to use the %u format specifier.
You can probably see that feof is mostly superfluous... You can usually test for erroneous reads by checking the return value. Try to avoid feof in the future.
Your main function has a return type of int, yet at the end of it you have a return; statement which doesn't return an int value. Remove that. It's probably causing errors during compilation.
Presumably, when argv != 3 you want to return from main so you don't end up processing invalid arguments... Make sure you return an int value, e.g.
if (argc != 3) {
printf("This program takes two arguments: the name of the file with the code, and the name of the file with the encoded message\n");
return 0;
}

Issues writing PNM P6

I'm writing a program that takes in two duplicate PNM P6 files, puts the memory of the first file into a buffer, creates a yellow diagonal line over that, and writes the result to the second file. When I run it, the output file is corrupted and can't be displayed. I noticed when looking at the output that it's missing the three lines that should be at the top:
P6
1786 1344
255
I don't know how to programmatically ensure that those lines stay in the code -- I can't figure out why they're even being overwritten in the first place.
When I manually add those lines to the output, the file is no longer corrupted. However, no yellow diagonal line appears. Is this part of the same issue, or is there something else I should look into fixing?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
unsigned char red, green, blue;
} iPixel;
typedef struct
{
int x, y;
iPixel *data;
} Image;
Image *
ReadImage(char *filename)
{
FILE *f = fopen(filename, "rb");
Image *img = NULL;
char magicNum[128];
int width, height, maxval;
if (f == NULL)
{
fprintf(stderr, "Unable to open file %s\n", filename);
return NULL;
}
fscanf(f, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval);
/*
printf("Magic num = %s width = %d, height = %d, maxval = %d\n",
magicNum, width, height, maxval)
;
*/
if (strcmp(magicNum, "P6") != 0)
{
fprintf(stderr, "Unable to read from file %s, because it is not a PNM file of type P6\n", filename);
return NULL;
}
img = (Image *) malloc(sizeof(Image));
img -> x = width;
img -> y = height;
img -> data = (iPixel*) malloc(img -> x * img -> y * sizeof(iPixel));
fread(img -> data, sizeof(iPixel), width*height, f);
fclose(f);
return img;
}
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
fwrite(img->data, sizeof(iPixel), img-> x * img-> y, f);
fclose(f);
}
Image *
YellowDiagonal(Image *input)
{
int i, j;
for (i = 0; i < input->x; i++)
{
for (j=0; j < input->y; j++)
{
if (i==j)
{
input->data[i].red=255;
input->data[i].green=255;
input->data[i].blue=0;
}
}
}
return input;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./3A_c.c <input image file> <output image file>\n");
exit(EXIT_FAILURE);
}
Image *img;
img = ReadImage(argv[1]);
fprintf(stderr, "Read.\n");
YellowDiagonal(img);
fprintf(stderr, "Diagonal line.\n");
WriteImage(img, argv[2]);
fprintf(stderr, "Write.\n");
}
Be careful to write exactly the same format you are reading. The PNM format is well defined, and you are reading it correctly. However, in the writing routine there were a couple of mistakes:
opening a file with "w" or "wb" truncates it to 0 bytes;
best practice is always to check if fopen succeeds;
reading actual ASCII data can be done with fscanf, binary data with fread. Similarly, writing ASCII should be done with fprintf and only binary data again with fwrite.
If you want to make sure you write the same data as you read in before, you need to save it somewhere. The maxval variable is read, but not saved, and so I cannot write it back. However, it is not a huge problem because the rest of your code assumes the image is R8G8B8 anyway, and so maxval should always be 255.
Here is an adjusted WriteImage that works.
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
if (f == NULL)
{
printf ("Unable to open '%s' for writing!\n", filename);
/* better would be: "return -1" to indicate an error, 0 otherwise */
return;
}
fprintf (f, "P6\n");
fprintf (f, "%d %d\n", img->x, img->y);
/* actually you need to write 'maxval' here */
fprintf (f, "%d\n", 255);
fwrite(img->data, sizeof(iPixel), img->x * img->y, f);
fclose(f);
}
With the above out of the way, you can now see your 'diagonal line' is not correct! I'm not going to fix that (I suppose not being able to see what happened stopped you in your tracks), but here are a few pointers just to guide you:
no need to return an Image * if you are changing data in situ anyway
no need to check every single pixel
check the coordinates of what pixels are changed ...

Using fscan() for a file, how can I do something with the values inside the file?

So if I have a file with several numbers, and I open the file using
fp = fopen (filename, "r");
So now I can read the contents of the file correct? How could I do something with the file. Like in this file there are numbers and I want to be able to add them up.
fscanf(fp)
would be the beginning of what is supposed to be correct? But I am not sure what to do beyond it. What code represents the items inside the file I am opening? If it is "x" then I want to add all the "x"'s up and then divide it by the total number of files there are.
How can I use the variables inside a file and do things with them?
Edited code:
if (fp != NULL)
{
while (fscanf(fp, "%lf", &d) == 1)
sum += d;
mean = sum / total;
printf ("The number of data values read from this file was %.0lf\n", total);
printf ("\n%.2lf\n", mean);
fclose(fp);
}
if (fp != NULL)
{
do
{
c = fgetc(fp);
if (c != EOF)
{
if ((char)c == '\n')
total++;
}
} while (feof(fp) == 0);
Simple example (not hardened) that adds the numbers read from a file...
#include <stdbool.h>
#include <stdio.h>
int main(int argc, char** argv)
{
float sum = 0;
float eachValue;
FILE *fp;
fp = fopen(argv[1], "r");
while (true)
{
int scanned = fscanf(fp, "%f\n", &eachValue);
if (scanned == 1)
{
sum += eachValue;
}
else
{
break;
}
}
fclose(fp);
printf("%f\n", sum);
}
For hardening, you should do things like deal with empty lines, validate there IS an arg[1] to be opened, deal with the cases where there's no file to be opened or some other error occurs during opening, etc.
If the only content in the file is valid numbers, this will work. Actually, it will work until the first text that isn't a valid number:
#include <stdio.h>
int main(void)
{
double sum = 0.0;
char const *filename = "data.file";
FILE *fp = fopen(filename, "r");
if (fp != 0)
{
double d;
while (fscanf(fp, "%lf", &d) == 1)
sum += d;
fclose(fp);
}
printf("%g\n", sum);
return(0);
}
As a general rule, if a function opens a file successfully, it should close it when it's finished. In this example, the system would close the file anyway, but it is as well to get into good habits. With a little more care and attention, you could distinguish between a format error (return value of 0 from fscanf()) and EOF or I/O error.
You can have these many functions to read from a file in general.
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int fscanf(FILE *restrict stream, const char *restrict format, ...);
size_t fread(void *restrict ptr, size_t size, size_t nitems,FILE *restrict stream);
depending upon your requirement use any one of this.
For example as you said , you have to add the numbers which are in the file.
So try reading from file with any of the above function into char array and then using delimiter (the character by which numbers are separated in file) parse the array into integer array or different numbers. Then it's easy to find the sum.
You should see man strtod
Also this man strtok

Resources