Adding Line break After Every Line - c

I have written the following piece of code:
#include<stdio.h>
void add_linebreak()
{
FILE *fp ;
char c, NEWL=10 ;
fp=fopen("text1.txt","a+") ;
if(fp==NULL)
{
printf("\nFile Not Found") ;
}
fseek(fp,0,SEEK_CURR) ;
while(!feof(fp))
{
//if(!feof(fp))
//{
c=fgetc(fp);
if(c==NEWL)
{
fprintf(fp,"%c",NEWL) ;
}
//}
}
fclose(fp) ;
}
int main()
{
add_linebreak() ;
printf("\nEditing Complete") ;
return 0 ;
}
The program took following data as input from a file named text1.txt :
1 this
2 is
3 a
4 text
5 file
6 to
7 test a
8 program
9 written
10 in c
Actual Output :
1 this
2 is
3 a
4 text
5 file
6 to
7 test a
8 program
9 written
10 in c
11
Expected Output:
1 this
2
3 is
4
5 a
6
7 text
8
9 file
10
11 to
12
13 test a
14
15 program
16
17 written
18
19 in c
20
I scratched my head on this for hours but wasn't able to get the expected output, please help me.

you gotta make a copyy of the file
Also fgetc returns int not char
void add_linebreak()
{
FILE *fin, *fout ;
char NEWL='\n' ;
fin=fopen("text1.txt","rb") ;
if(fp==NULL)
{
printf("\nFile Not Found") ;
}
fout = fopen("copy.txt","w");
while(1)
{
int c=fgetc(fin);
if(c==EOF)
break;
fprintf(fout, "%c", c);
if(c==NEWL)
{
fprintf(fout,"%c",NEWL) ;
}
}
fclose(fout) ;
fclose(fin);
}
renaming the file back to the original and adding some more error handling is left as a test :-)

You may not perform the modifications in-place. Not with stuff like text.
You have to instead read from one file, write to a temporary then perhaps rename the copy to overwrite the original update.
The other answer says almost everything besides the rename.
Oh, another thing: I have found often that newlines tended to get stripped in my old test programs; that may be another thing. But the modifying-in-place is the most likely culprit.

Related

fscanf() causing segmentation fault

I am writing some code in C to read some file data in arrays and keep getting a segmentation fault compiling with gcc. It reads the file up to 11th line of data then gives the fault. Been through some other similar questions on here but can't find a solution.
Thanks
code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int ainb(char a[],char b[])//returns 0 if str b contains a returns 1 otherwise
{
int i=0,j0=-1,j1=0,count=0;
if(strlen(b)<strlen(a)) {return 1;}
for(i=0;i<strlen(b);i++) {
if((b[i]==a[j1])&&(j1==j0+1)){
j0=j1;j1++;
} else {
j0=-1;j1=0;
}
if((j1+1)==strlen(a)) {break;}
}
if((j1+1)==strlen(a)){
return 0;
} else {
return 1;
}
}
void read_pdb(FILE* fp,char **atm,int *atnum,char **name,char **res,char *chain,int *resnum,double *x,double *y,double *z,double *occ,double *bfac,char **elem,double ac[2][3]) //reads file lines and stores in arrays
{
printf("\nReading pdb data\n");
int i=0,j=0;
char buff[7];
fpos_t position;
while(!feof(fp))
{
fgetpos(fp,&position);fgets(buff,sizeof buff,fp);
if((ainb("ATOM",buff)==0)||(ainb("HETATM",buff)==0))
{
fsetpos(fp,&position);printf("\ngetting position %d\n",i+1);
fscanf(fp,"%6s%5d %4s %3s %1s%4d %8lf%8lf%8lf%6lf%6lf %2s \n",atm[i],&atnum[i],name[i],res[i],&chain[i],&resnum[i],&x[i],&y[i],&z[i],&occ[i],&bfac[i],elem[i]);
printf("\nnode %d data found\n",i+1);
printf("\n%6s%5d %4s %3s %1s%4d %8.3lf%8.3lf%8.3lf%6.2lf%6.2lf %2s \n",atm[i],atnum[i],name[i],res[i],&chain[i],resnum[i],x[i],y[i],z[i],occ[i],bfac[i],elem[i]);
if(ainb("HETATM",atm[i])==0){
ac[j][0]=x[i];ac[j][1]=y[i];ac[j][2]=z[i];j++;
}
i++;
}
}
printf("\n%d Atoms read\n",i);
}
void main()
{
double ac[2][3];
int N,k;
double *x,*y,*z,*occ,*bfac;
char **atm,**name,**res,**elem,*chain;
int *atnum,*resnum;
FILE *out;
out=fopen("OUT.pdb","r");//something to check for file
N=66;
//make dynamic arrays
x=(double*)malloc(N*sizeof(double));
y=(double*)malloc(N*sizeof(double));
z=(double*)malloc(N*sizeof(double));
occ=(double*)malloc(N*sizeof(double));
bfac=(double*)malloc(N*sizeof(double));
atnum=(int*)malloc(N*sizeof(int));
resnum=(int*)malloc(N*sizeof(int));
atm=(char**)malloc(N*sizeof(char));
name=(char**)malloc(N*sizeof(char));
res=(char**)malloc(N*sizeof(char));
elem=(char**)malloc(N*sizeof(char));
chain=(char*)malloc(N*sizeof(char));
for(k=0;k<N;k++)
{
atm[k]=(char*)malloc(7*sizeof(char));
name[k]=(char*)malloc(5*sizeof(char));
res[k]=(char*)malloc(4*sizeof(char));
elem[k]=(char*)malloc(3*sizeof(char));
}
//read in data
read_pdb(out,atm,atnum,name,res,chain,resnum,x,y,z,occ,bfac,elem,ac);
fclose(out);
printf("\n-------------------------------------------\nTest Complete\n");
free(x);
free(y);
free(z);
free(occ);
free(bfac);
free(elem);
free(name);
free(atm);
free(res);
free(resnum);
free(atnum);
free(chain);
}
The output is:
Reading pdb data
getting position 1
node 1 data found
ATOM 1 CA PRO A 1 4.612 0.903 5.089 1.00 24.97 C
getting position 2
node 2 data found
ATOM 2 CA SER A 2 3.526 0.341 3.809 1.00 59.99 C
getting position 3
node 3 data found
ATOM 3 CA ARG A 3 6.208 1.550 6.551 1.00 20.40 C
getting position 4
node 4 data found
ATOM 4 CA TRP A 4 5.912 2.348 4.388 1.00 50.28 C
getting position 5
node 5 data found
ATOM 5 CA GLE A 5 4.087 4.359 6.884 1.00 54.04 C
getting position 6
node 6 data found
ATOM 6 CA THR A 6 4.405 1.292 2.566 1.00 62.06 C
getting position 7
node 7 data found
ATOM 7 CA TYR A 7 3.327 3.041 5.205 1.00 50.46 C
getting position 8
node 8 data found
ATOM 8 CA VAL A 8 5.276 0.109 0.387 1.00 58.00 C
getting position 9
node 9 data found
ATOM 9 CA LEU A 9 2.992 3.190 3.084 1.00 41.48 C
getting position 10
node 10 data found
ATOM 10 CA CYS A 10 3.565 0.287 0.721 1.00 47.65 C
getting position 11
Segmentation fault (core dumped)
Lets consider this code:
name=(char**)malloc(N*sizeof(char));
for(k=0;k<N;k++)
{
name[k]=(char*)malloc(5*sizeof(char));
}
You allocate n*sizeof(char) array and try to store N pointers to char in it. But size of pointer to the char is greater than sizeof(char), so you get buffer overflow and undefined behavior even on the initialization stage. You are lucky and your program is not crashing at this stage, but it will fail on the array usage. To prevent this error you should use sizeof(char*) in your allocation code.
Rather than hard code the type, and get it wrong for name, let the compiler figure it out. Less code, easier to read and easier to code & maintain.
//bfac=(double*)malloc(N*sizeof(double));
//resnum=(int*)malloc(N*sizeof(int));
//name=(char**)malloc(N*sizeof(char)); OP was looking for `sizeof (char*)`
bfac = malloc(N * sizeof *bfac);
resnum = malloc(N * sizeof *resnum);
name = malloc(N * sizeof *name);
Also in C, no need to cast the result of malloc().

C: Write to a specific line in the text file without searching

Hello I have file with text:
14
5 4
45 854
14
4
47 5
I need to write a text to a specific line. For example to the line number 4 (Doesn't matter whether I will append the text or rewrite the whole line):
14
5 4
45 854
14 new_text
4
47 5
I have found function fseek(). But in the documentation is written
fseek(file pointer,offset, position);
"Offset specifies the number of positions (bytes) to be moved from the location specified bt the position."
But I do not know the number of bites. I only know the number of lines. How to do that? Thank you
You can't do that, (text) files are not line-addressable.
Also, you can't insert data in the middle of a file.
The best way is to "spool" to a new file, i.e. read the input line by line, and write that to a new file which is the output. You can then easily keep track of which line you're on, and do whatever you want.
I will assume that you are going to be doing this many times for a single file, as such you would be better indexing the position of each newline char, for example you could use a function like this:
long *LinePosFind(int FileDes)
{
long * LinePosArr = malloc(500 * sizeof(long));
char TmpChar;
long LinesRead = 0;
long CharsRead = 0;
while(1 == read(FileDes, &TmpChar, 1))
{
if (!(LinesRead % 500)
{
LinePosArr = realloc(LinePosArr, (LinesRead + 500) * sizeof(long));
}
if (TmpChar == '\n')
{
LinePosArr[LinesRead++] = CharsRead;
}
CharsRead++;
}
return LinePosArr;
}
Then you can save the index of all the newlines for repeated use.
After this you can use it like so:
long *LineIndex = LinePosFind(FileDes);
long FourthLine = LineIndex[3];
Note I have not checked this code, just written from my head so it may need fixes, also, you should add some error checking for the malloc and read and realloc if you are using the code in production.

Gnuplot and C "x range is invalid"

I'm trying to generate a data file and to plot it with Gnuplot. The problem is when I keep my Nstep lower than 348 I get the error
line 0: warning: Skipping data file with no valid points
plot 'plot.txt' using 1:2 with lines
^
line 0: x range is invalid
But I keep the Nstep higher than 348 everything works fine. I do not understand why. Here is my C code:
int main(void){
int Nstep = 348;
//omitted part...
FILE *pipe = fopen("plot.txt", "w+");
while (n<Nstep) {
pos[n+1] = pos[n] + v[n]*h;
v[n+1] = v[n] + h * Fx(pos[n]);
fprintf(pipe, "%d %05.3lf\n", n, v[n]);
n++;
}
close(pipe);
system("gnuplot -p -e \"plot 'plot.txt' using 1:2 with lines\"");
return 0;
}
plot.txt example (Nstep = 10)
1 100.000
2 99.000
3 97.000
4 94.010
5 90.050
6 85.150
7 79.349
8 72.697
9 65.252
10 57.079
I am unable to replicate your error as you didn't include the full source (function Fx and definitions of pos and v). You are calling the wrong close. You should call fclose() (this will flush the file handle too).
fclose(pipe)
And not
close(pipe)
You could explicitly flush the data by calling fflush().

c: fopen and fprint

as I understood, In the next code:
int main () {
FILE * f1;
f1 = fopen("f1.txt","a");
for (i =0 ; i<10;i++) fprintf(f1,"%d ",i);
fclose(f1);
f1 = fopen("f1.txt","a");
for (i =0 ; i<10;i++) fprintf(f1,"%d ",i);
fclose(f1);}
I will get in File f1, the next serial: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9. I didn't understand why. When I close the file, and open it again, it doesn't remember the end file. I expected that the second loop will override the text that was there before, and I will get just 0 1 2 3 4 5 6 7 8 9. So - what happened?
It's because you open the file in mode "a", which stands for append. The new text gets added to the end of the file.
If you want to write over what's already there, replace the second fopen with:
f1 = fopen("f1.txt", "w");
"w" stands for write, and will replace what's already there with your new text.
"a" means append; perhaps you want "w" (write) instead?
You opened the file in append-mode when you passed "a" as the second argument to fopen, so it appended the data.

jumping to the a wanted line and overriding the first letter inside it in c

hello i am trying to write to a FILE in a wanted line number using c programming language
and for some unknown reasons it doesnt get written
this is my checking code:
int main()
{
int x;
int counter = 0;
char buffer[MAX];
FILE* fp = fopen("sale_day.txt","w");
fprintf(fp,"5 orange 11\n");
fprintf(fp,"4 pelephone 222\n");
fprintf(fp,"3 mirs 4000\n");
fprintf(fp,"2 cellcom 302\n");
fprintf(fp,"1 tmobile 500\n");
fclose(fp);
fp = fopen("sale_day.txt","r+");
while (counter < 2)
{// jumping two rows
fgets(buffer,MAX,fp);// i tried using fscanf which didnt help aswell
counter++;
}
fflush(fp); // i tried with and without still doesnt work
fputs("$",fp);
fflush(fp); // i tried with and without still doesnt work
fclose(fp);
}
i expect to get :
5 orange 11
4 pelephone 222
$ mirs 4000
2 cellcom 302
1 tmobile 500
for some reason it stays as the following in "sale_day.txt" file
5 orange 11
4 pelephone 222
3 mirs 4000
2 cellcom 302
1 tmobile 500
even tho when i debug it it shows a "$" instead of the 3 digit
thanks in advance for your help !
The code works just fine, also without the fflush-lines. After running the program, line 3 is changed as follows:
Before:
3 mirs 4000
After:
$ mirs 4000
I ran your code like it is, only with this on top:
#include <stdio.h>
#define MAX 255
What are you expecting? When I run your code, I get:
5 orange 11
4 pelephone 222
$ mirs 4000
2 cellcom 302
1 tmobile 500
This is exactly right given what you've coded: read two lines (leaving you positioned at the third line), and write a $ char.
Note that file write operations overwrite existing file data, or append new data to the end of a file. They don't insert data (which may have been the operation you're expecting).

Resources