G
grinder
i am writing a prog that reads mulitple lines from a txt file in
succession into code that takes these lines as an input file that would
be opened and from which data would be manipulated. As the in the
following:
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 150
#define PI 3.14159265
void stripnl(char *str) {
while(strlen(str) && ( (str[strlen(str) - 1] == 13) ||
( str[strlen(str) - 1] == 10 ))) {
str[strlen(str) - 1] = 0;
}
}
int main() {
char fname[40];
char line[20];
double x[MAX],y[MAX];
double mm,mxy,my,mx,mx2;
double resul;
double a[2][2],b[2][2],c[2][2];
double w;
int q,counter,v,lcount;
int ifail;
int m,n,k;
FILE *input,*result,*infile;
// Read in the filename //
printf("Enter the name of data file: ");
fgets(fname, sizeof(fname), stdin);
// get rid of the newline char //
stripnl(fname);
strtok(fname," ");
// Open the file//
if((infile = fopen(fname, "rt")) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
while( fgets(line, sizeof(line), infile) != NULL ) {
// Get each line from the infile //
lcount++;
// print the line number and data //
printf("Line %d: %s",lcount,line);
printf("%s",line);
<><>><><><><><><><><><><><><><><><><><>
if ((input = fopen(line,"rt")) == NULL )
{
printf("\ndata file not found\n\n");
exit(0);
}
<><><><><><><><><><><><><><><><><><><><><><
counter=0;
while(!feof(input))
{
if(counter==MAX-1)
{
printf("\n too many points ,taking the first %d points
\n",counter); break;
}
counter++;
fscanf(input,"%lf %lf\n",&x[counter],&y[counter]);
printf("%lf %lf\n",&x[counter],&y[counter]);
}
v=counter;
result=fopen("st_line.dat","w");
for(q=1,mm=0,mx=0,mxy=0,my=0,mx2=0;q<=v;q++)
{
//summing values and naming as variable//
{
resul=cos(x[q]*PI/180);
printf(" %lf %lf \n",resul,x[q]);
}
mm=q;
mxy=mxy+resul*y[q];
my=my+y[q];
mx=mx+resul;
mx2=mx2+resul*resul;
}
a[0][0]= mm;
a[0][1]= mx;
a[1][0]= mx;
a[1][1]= mx2;
b[0][0]=my;
b[1][0]=mxy;
b[0][1]=0;
b[1][1]=0;
printf("\n\n %lf %lf \n %lf %lf
\n",a[0][0],a[0][1],a[1][0],a[1][1]);
printf("\n\n %lf %lf \n %lf %lf
\n",b[0][0],b[0][1],b[1][0],b[1][1]);
printf("\n\n %lf %lf \n %lf %lf
\n\n",a[0][0],a[0][1],a[1][0],a[1][1]);
fclose(input);
fclose(result);
}
#undef MAX
fclose(infile);
}
---------------------------------------------------------------------------------
When it gets to the "if" statement between the "<><><><><><" i receive
the "data file not found", but yet in the fopen line that contains
"fname" works perfectly if the "stripnl()" line is present. This is my
first time working with char strings in this fasion and i am not
familiar with the procedures. i assumed that it is because fopen does
not know where the end of the string is and is getting confused, and if
that is the case, then i am not versed well enough in C to tell it
otherwise.
succession into code that takes these lines as an input file that would
be opened and from which data would be manipulated. As the in the
following:
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 150
#define PI 3.14159265
void stripnl(char *str) {
while(strlen(str) && ( (str[strlen(str) - 1] == 13) ||
( str[strlen(str) - 1] == 10 ))) {
str[strlen(str) - 1] = 0;
}
}
int main() {
char fname[40];
char line[20];
double x[MAX],y[MAX];
double mm,mxy,my,mx,mx2;
double resul;
double a[2][2],b[2][2],c[2][2];
double w;
int q,counter,v,lcount;
int ifail;
int m,n,k;
FILE *input,*result,*infile;
// Read in the filename //
printf("Enter the name of data file: ");
fgets(fname, sizeof(fname), stdin);
// get rid of the newline char //
stripnl(fname);
strtok(fname," ");
// Open the file//
if((infile = fopen(fname, "rt")) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
while( fgets(line, sizeof(line), infile) != NULL ) {
// Get each line from the infile //
lcount++;
// print the line number and data //
printf("Line %d: %s",lcount,line);
printf("%s",line);
<><>><><><><><><><><><><><><><><><><><>
if ((input = fopen(line,"rt")) == NULL )
{
printf("\ndata file not found\n\n");
exit(0);
}
<><><><><><><><><><><><><><><><><><><><><><
counter=0;
while(!feof(input))
{
if(counter==MAX-1)
{
printf("\n too many points ,taking the first %d points
\n",counter); break;
}
counter++;
fscanf(input,"%lf %lf\n",&x[counter],&y[counter]);
printf("%lf %lf\n",&x[counter],&y[counter]);
}
v=counter;
result=fopen("st_line.dat","w");
for(q=1,mm=0,mx=0,mxy=0,my=0,mx2=0;q<=v;q++)
{
//summing values and naming as variable//
{
resul=cos(x[q]*PI/180);
printf(" %lf %lf \n",resul,x[q]);
}
mm=q;
mxy=mxy+resul*y[q];
my=my+y[q];
mx=mx+resul;
mx2=mx2+resul*resul;
}
a[0][0]= mm;
a[0][1]= mx;
a[1][0]= mx;
a[1][1]= mx2;
b[0][0]=my;
b[1][0]=mxy;
b[0][1]=0;
b[1][1]=0;
printf("\n\n %lf %lf \n %lf %lf
\n",a[0][0],a[0][1],a[1][0],a[1][1]);
printf("\n\n %lf %lf \n %lf %lf
\n",b[0][0],b[0][1],b[1][0],b[1][1]);
printf("\n\n %lf %lf \n %lf %lf
\n\n",a[0][0],a[0][1],a[1][0],a[1][1]);
fclose(input);
fclose(result);
}
#undef MAX
fclose(infile);
}
---------------------------------------------------------------------------------
When it gets to the "if" statement between the "<><><><><><" i receive
the "data file not found", but yet in the fopen line that contains
"fname" works perfectly if the "stripnl()" line is present. This is my
first time working with char strings in this fasion and i am not
familiar with the procedures. i assumed that it is because fopen does
not know where the end of the string is and is getting confused, and if
that is the case, then i am not versed well enough in C to tell it
otherwise.