Gnuplot and C "x range is invalid" - c

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().

Related

How to read one line at a time from a data file and to perform calculations in it before moving to the next line in C Programming?

I'm a beginner at C programming and I would appreciate some help in order to understand the problem.
Alright so, I have a data file (input.dat) with data like this: (first line) 0 2 3 4 5; (second line) 1 2 3 5 4, (third line and so on...). I'm required to read the data one line at a time until the end of file and print it. This is what I have done so far:
int main(void)
{
float coeffs[5];
FILE *input; /* File pointer to the input file */
fopen_s(&input, "input.dat", "r"); /* Location of the input file */
int count = 0;
/* Loops to read data set*/
while (fscanf_s(input, "%f %f %f %f %f ", &coeffs[0], &coeffs[1], &coeffs[2], &coeffs[3], &coeffs[4]) != EOF)
{
printf("a=%.4f; b=%.4f; c=%.4f; d=%.4f; e=%.4f\n", coeffs[0], coeffs[1], coeffs[2], coeffs[3], coeffs[4]);
count++;
}
return 0;
}
This is showing all of the lines in the data file at once. But this is not what I want. I need to read one line at a time and perform some calculations and conditions for that one line first before I move to the next line. So how can I do that?
Next problem is, for the first line, I need to implement a loop from -10 to +10 with increment of 2 (to get 11 results in total). For example the program will read the first line, display it on the screen, then for the first value -10, the program will calculate and again display something . Then it will do the same for -8, then for -6 and so on until +10. After the 11 results are displayed, the program will then and ONLY then, move to the second line and so on. Hence for each line in the data file, the program will have 11 results. How can I use the loop function with increment of 2 to achieve these 11 results?
I would appreciate if anyone can provide me a simple layout of the structure of the codes which I've to write. NOTE: The formats are a bit different than other compilers as I must use Microsoft Visual Studio to do it.
Add your calculations to your while loop. You are reading one line at a time anyway.
If you want to loop from -10 to 10 with increments of 2, use a for loop.
for(count = -10; count <= 10; count = count + 2)
{
// Calculations
}

Adding Line break After Every Line

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.

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.

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