Click here to Skip to main content
14,981,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am doing a assignment. The task is:

Quote:
Open a text file and print its contents in command window, ask the user to replace the text from the text file to be written and save the text file.


The program works find till the character length 170 and above. when I define the character below 170 it is showing segmentation fault, that too when I open debugger and check the error. Are there any ways to overcome this problem. what are changes do I need to perform for smaller lengths.


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

#define MAX 170

  int main()
  {
        FILE *fptr1, *fptr2;
        int lno, linectr = 1;
        char str,fname[MAX];
        char newln[MAX], temp[] = "temp.txt";

		printf("\n\n Replace a specific line in a text file with a new text :\n");
		printf(" Input the file name to be opened : ");
        fgets(fname, MAX, stdin);
        fname[strlen(fname) - 1] = '\0';
        fptr1 = fopen(fname, "r");
        if (!fptr1) {
                printf("Unable to open the input file!!\n");
                return 0;
                    }
        printf("Content of the file: \n\n");
        while ((str = getc(fptr1)) != EOF)
        putchar(str);

        fptr2 = fopen(temp, "w");
        if (!fptr2)
        {
                printf("Unable to open a temporary file to write!!\n");
                fclose(fptr1);
                return 0;
        }

        printf(" Input the content of the new line : ");
        fgets(newln, MAX, stdin);

        printf(" Input the line no you want to replace : ");
        scanf("%d", &lno);
        lno++;
        rewind(fptr1);
        while ((fgets(str, MAX, fptr1))!=NULL)
        {
                linectr++;
                if (linectr == lno)
                        fputs(newln, fptr2);
                    else
                        fputs(str, fptr2);
        }
        fclose(fptr1);
        fclose(fptr2);
        remove(fname);
        rename(temp, fname);
        printf(" Replacement did successfully \n");
        return 0;
  }


What I have tried:

I have tried understanding different programs with same fault but didn't get clear view.
I have been trying to change the code but its not working completely, as of now it is working partially.
Posted
Updated 21-Jul-21 23:40pm
v3

First, you need to know that C manage nothing and forgive nothing, you are responsible of everything.
Quote:
The program works find till the character length 170 and above. when I define the character below 170 it is showing segmentation fault

This is typical of array or buffer overrun, which using (read or write) beyond the end of array or buffer. It is not complicated to understand that 170 is the size of your buffer.
This line says:
C++
fscanf(fptr1, "%[^\0]", str);

read the file in 1 go, no matter the size of file, no need to wonder why you get a segmentation fault.
Devil lays in details, so understanding that scanf will read data from a file is not complicated, but you need to study documentation to understand how to tell it to limit the size of the read. Or check the size of file before reading it.
scanf - C++ Reference[^]
   
Comments
CPallini 22-Jul-21 2:11am
   
5.
Patrice T 22-Jul-21 3:53am
   
Thank you
Sanre 22-Jul-21 5:38am
   
I have realized that reading a file in 1 go will surely occur segmentation fault. So I have changed that part of program using putchar function. Still I am facing 'Program received signal SIGSEGV, Segmentation fault' right after the line of rewind(fptr1).
You need to use the debugger repeatedly: first time to find exactly what is giving the segmentation fault, and then again to work out why.

Probably, it's to do with your MAX value - since it's 170 and that's the point at which it starts to give problems, it's likely that something is exceeding the space you allocated.
But only you can tell what and where, as we don't have your data files to check anything!

SO use the debugger to work out what is giving the error, then step through your code to find out why - we can't do that for you!
   
Comments
Sanre 22-Jul-21 5:42am
   
My file is less than 1 KB. I have received 'Program received signal SIGSEGV, Segmentation fault' during my debugging operation.
OriginalGriff 22-Jul-21 6:32am
   
So what? The size doesn't matter here, it's how the data is organised and what you do with it that causes the problem.

The debugger is the only way to find where the problem is happening, what data is invlov ed and then look to find out why.

And we can't do that for yoU!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900