这里有一些可疑的东西与stripnl() [英] something fishy here with stripnl()

查看:62
本文介绍了这里有一些可疑的东西与stripnl()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,从

继承中的txt文件中读取多行代码,将这些行作为输入文件,打开并从中打开
哪些数据会被操纵。如下面的




#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 *输入,*结果,* infile;


//读入文件名//

printf("输入数据文件的名称:" );

fgets(fname,sizeof(fname),stdin);


//摆脱newlin e char //

stripnl(fname);

strtok(fname," ");


//打开文件//

if((infile = fopen(fname," rt"))== NULL) {

printf(错误打开文件。\ n);

退出(1);

}

while(fgets(line,sizeof(line),infile)!= NULL){

//从infile获取每一行//

lcount ++;

//打印行号和数据//

printf("行%d:%s",lcount,行);

printf("%s",line);


<><>><><><><>< ;><><><><><><><><><><><><> ;


if((input = fopen(line," rt"))== NULL)

{

printf (" \\\ ndata file not\\\
\ n");

退出(0);

}

< ><><><><><><><><><><><><> <><><><><>< ;><><><


counter = 0;

while(!feof(input))

{

if(counter == MAX-1)

{

printf(\ n太多积分,服用第一个%d点

\ n",counter);休息;

}

counter ++;

fscanf(输入,"%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 ++)

{

//求和值和命名为变量//
{

结果= cos(x [q] * PI / 180);

printf(" ;%lf%lf \ n",结果,x [q]);

}

mm = q;

mxy = mxy +结果* y [q];

my = my + y [q];

mx = mx +结果;

mx2 = mx2 +结果*结果;

}

a [0] [0] = mm;

a [0] [1] = mx;

a [1] [0] = mx;

a [1] [1] = mx2;


b [0] [0] =我的;

b [1] [0] = mxy;

b [0] [1] = 0;

b [1] [1 ] = 0;


printf(" \ n \ n%lf%lf \ n%lf%lf

\ n",一个[0] [0],A [0] [1],A [1 ] [0],a [1] [1]);

printf(" \ 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 \ nn,a [0] [0],a [0] [1],a [1] [0],a [1] [1]);


fclose(输入);

fclose(结果) ;

}


#undef MAX

fclose(infile);

}


----------------------------------------- ----------------------------------------


当它到达if时<><><><><><"之间的陈述我收到了未找到数据文件的

,但是在包含

" fname"的fopen行中如果stripnl()可以完美地工作。线存在。这是我的

第一次使用此字符串中的字符串,我不熟悉这些程序。我认为这是因为fopen确实

不知道字符串的结尾在哪里并且感到困惑,如果

就是这种情况,那我就不精通了好吧在C中告诉它

否则。

解决方案

" grinder" < tp ** @ pitt.eduwrites:


void stripnl(char * str){

while(strlen(str)& &(;(str [strlen(str) - 1] == 13)||

(str [strlen(str) - 1] == 10))){

str [strlen(str) - 1] = 0;

}


}



你应该使用''\ r''和''\ n'代替13和10.事实上,

''\ r''不应该是必要的,因为你正在阅读文本

流,文本流中的行只是换行(''\ n'')。


函数的名称不应该是stripnl,因为名字

以str开头后跟一个字母保留。


它是'调用strlen这么多

次可能真的效率低下。你应该只需要为每个函数调用一次

调用。


过度使用上面的括号使得实际难以实现

读出运算符优先级。我建议把等式表达式的

括号作为开头。


我不确定真正的问题是什么,因为我只读了你的大部分代码,但是那些应该让你开始。

-

给我几年时间一项大型研究补助金,

,我会给你一张收据。 --Richard Heathfield


Ben Pfaff写道:


" grinder" < tp ** @ pitt.eduwrites:


> void stripnl(char * str){
while(strlen(str)&& ((str [strlen(str) - 1] == 13)||
(str [strlen(str) - 1] == 10))){
str [strlen(str) - 1 ] = 0;
}

}



您应该使用''\ r''和''\ n ''代替13和10.事实上,

''\ r''不应该是必要的,因为你正在阅读文本

stream ,文本流中的行只以新行结尾(''\ n'')。



在现实世界中,人们经常在Unix上遇到MS-DOS文本文件或者

类似Unix的(例如Cygwin)系统。那里的文本流不会被删除

给你的''\ r''。


在许多情况下,编写代码是一种好习惯这将正常工作

是否遇到Unix(\ n),Mac(\r)或MS-DOS(\\ n)文本

文件,甚至是一个文件中的混合文件。


-

Simon。


"磨床" < tp ** @ pitt.eduwrites:


i我正在编写一个程序,从

继承中的txt文件中读取多行代码到代码中将这些行作为输入文件打开,打开并从中操作数据。如下面的




#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;

}



可能效率低下。像这样的东西可能更快更容易读取/调试
。没有经过测试 - 你明白了。我猜你有多个空行的可能性是多少?


size_t l = strlen(str);

while(l &&(str [l-1] ==''\ n''))

str [l--] =''\''';

至于其余部分:您是否使用调试器/ printf来检查线路?变量?

它到底有没有?如果你打电话给stripnl会有用吗?


更重要的是,你确定它试图打开的文件在那里吗? : - ;


祝你好运!


>

}


int main(){

char fname [40];

char line [20];

double x [MAX],y [MAX];

双mm,mxy,my,mx,mx2;

双结果;

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;


//读入文件名//

printf("输入数据文件的名称:");

fgets (fname,sizeof(fname),stdin);


//摆脱换行符//

stripnl(fname);

strtok(fname,"");


//打开文件//

if((infile = fopen(fname, rt))== NULL){

printf(错误打开文件。\ n);

退出(1);

}

而( fgets(line,sizeof(line),infile)!= NULL){

//从infile获取每一行//

lcount ++;

//打印行号和数据//

printf("行%d:%s",lcount,行);

printf("% s",line);


<><>><><><><><>< ><><><><><><><><><><><>


if((input = fopen(line," rt"))== NULL)

{

printf(" \未找到ndata文件\ n \\ nn \\;);

退出(0);

}

<><> ;<><><><><><><><><><><><>< ;><><><><><><><


counter = 0;

while(!feof(输入))

{

if(counter == MAX-1)

{

printf(\ n太多积分,取第​​一个%d点

\ n,计数器);休息;

}

counter ++;

fscanf(输入,"%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 ++)

{

//求和值和命名为变量//
{

结果= cos(x [q] * PI / 180);

printf(" ;%lf%lf \ n",结果,x [q]);

}

mm = q;

mxy = mxy +结果* y [q];

my = my + y [q];

mx = mx +结果;

mx2 = mx2 +结果*结果;

}

a [0] [0] = mm;

a [0] [1] = mx;

a [1] [0] = mx;

a [1] [1] = mx2;


b [0] [0] =我的;

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\\\
%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 \ nn",a [0 ] [0],a [0] [1],a [1] [0],a [1] [1]);


fclose(输入);

fclose(结果);

}


#undef MAX

fclose(infile);

}


-------------------------------- -------------------------------------------------


当它到达if时<><><><><><"之间的陈述我收到了未找到数据文件的

,但是在包含

" fname"的fopen行中如果stripnl()可以完美地工作。线存在。这是我的

第一次使用此字符串中的字符串,我不熟悉这些程序。我认为这是因为fopen确实

不知道字符串的结尾在哪里并且感到困惑,如果

就是这种情况,那我就不精通了C语言告诉它

否则。


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.

解决方案

"grinder" <tp**@pitt.eduwrites:

void stripnl(char *str) {
while(strlen(str) && ( (str[strlen(str) - 1] == 13) ||
( str[strlen(str) - 1] == 10 ))) {
str[strlen(str) - 1] = 0;
}

}

You should use ''\r'' and ''\n'' in place of 13 and 10. In fact, the
''\r'' should not be necessary, because you''re reading a text
stream, and lines in text streams end in just a new-line (''\n'').

The name of the function should not be stripnl, because names
that begin with str followed by a letter are reserved.

It''s likely to be really inefficient to call strlen so many
times. You should only need to call it once per function
invocation.

The excessive use of parentheses above makes it hard to actual
read out the operator precedence. I''d suggest dropping the
parentheses around the equality expressions as a start.

I''m not sure what the real problem is, because I only read that
much of your code, but those should get you started.
--
"Give me a couple of years and a large research grant,
and I''ll give you a receipt." --Richard Heathfield


Ben Pfaff wrote:

"grinder" <tp**@pitt.eduwrites:

>void stripnl(char *str) {
while(strlen(str) && ( (str[strlen(str) - 1] == 13) ||
( str[strlen(str) - 1] == 10 ))) {
str[strlen(str) - 1] = 0;
}

}


You should use ''\r'' and ''\n'' in place of 13 and 10. In fact, the
''\r'' should not be necessary, because you''re reading a text
stream, and lines in text streams end in just a new-line (''\n'').

In the real world, one does often encounter MS-DOS text files on Unix or
Unix-like (eg. Cygwin) systems. Text streams there will not strip out
the ''\r'' for you.

In many cases it''s good practise to write code that will work properly
whether it encounters Unix ("\n"), Mac ("\r") or MS-DOS ("\r\n") text
files, or even a mixture within one file.

--
Simon.


"grinder" <tp**@pitt.eduwrites:

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;
}

Possibly inefficient. Something like this is probably quicker and easier
to read/debug. Not tested - you get the idea. Youre assuming the
possibility of multiple blank lines I guess?

size_t l = strlen(str);
while(l && (str[l-1]==''\n''))
str[l--] = ''\0'';
As for the rest : did you use a debugger/printf to examine the "line" variable?
Does it have \n on the end? Does it work if you call stripnl?

More importantly, are you sure the files its trying to open are there? :-;

Good luck!

>
}

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.


这篇关于这里有一些可疑的东西与stripnl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆