读取文件并将其复制到数组中...... [英] reading file and copying it into array...

查看:65
本文介绍了读取文件并将其复制到数组中......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

im试图从一个文件中读取然后将其复制到一个数组中...我的

代码如下..我运行正常但是我不明白它没有显示

我任何输出?

这里是我的代码......

使用命名空间std;

int Adj [MAX] [MAX]; //相邻节点的矩阵

int curr [MAX]; //它给出当前索引来检索节点的

neigbours

int n = 100;

void read_file(FILE *);

int main()

{

FILE * pf;

int i,r;


如果((PF =的fopen(QUOT; nick.txt"," R"))== NULL){

的printf(QUOT;错误打开nick.txt文件);

返回1;

}


read_file(pf); //创建矩阵的函数Adj [] []


next_neighbour(1);

返回0;

}


void read_file(FILE * fd)

{


char buffer [500];
<登记/>
INT吨;

如果(的fscanf(FD,"!%d\\\
",&安培; n))的

的printf(QUOT ;读取文件的错误);

for(int i = 1; i< = n; i ++){

fgets(buffer,sizeof(buffer),fd) ;


t = 0;

for(int j = 0; j< n; j ++){

printf(" %d",buffer [j] - ''0'');


if((buffer [j] - ''0'')== 1)


{

Adj [i] [++ t] = j + 1;


printf("%d ",Adj [i] [j]);

//cout<<Ad[i][j].....but nothin works :(

}


}

// printf(" \ n");

// printf (" \ n");


curr [i] = 1;

}


fclose (fd);


}

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

我的文件内容如下所示:

nick.txt:

0 3 -1 3 -1 -1 -1

3 0 2 -1 -1 -1 - 1

-1 2 0 2 2 -1 -1

3 -1 2 0 6 -1 -1

-1 -1 2 6 0 4 3

-1 -1 -1 -1 4 0 -1

-1 -1 -1 -1 3 -1 0

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

i希望这个输入存储在一个数组中....如何读取这个

文件并将其存储在数组Adj ?????? any帮助将非常好

赞赏...

问候。

Hi all,
i m trying to read from a file and then copy it into an array...my
code is as follow..it runs fine but i cant understand y it doesnt show
me any output??
here is my code...
using namespace std;
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the
neigbours of a node
int n=100;
void read_file(FILE* );
int main()
{
FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}

read_file(pf); //function to create the matrix Adj[][]

next_neighbour(1);
return 0;
}

void read_file(FILE *fd)
{

char buffer[500];

int t;
if (!fscanf (fd,"%d\n",&n))
printf("error reading the file");
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);

t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-''0'');

if((buffer[j]-''0'')== 1)

{
Adj[i][++t]= j+1;

printf(" %d ", Adj[i][j]);
//cout<<Ad[i][j].....but nothin works :(

}

}
//printf("\n");
//printf("\n");

curr[i]= 1;
}

fclose(fd);

}
-----------------------------------------
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
--------------------------------------------------
i want this input to b stored inside an array....how can i read this
file and store it inside array Adj??????any help will b greatly
appreciated...
regards.

推荐答案

在文章< 11 ********************** @ e56g2000cwe.googlegroups .com> ;,

" Shuch" <嘘********** @ gmail.com>写道:
In article <11**********************@e56g2000cwe.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
大家好,我试图从一个文件中读取然后将其复制到一个数组中...我的
代码如下...运行正常,但我不明白,它没有显示任何输出?
这是我的代码...
使用命名空间std;
int Adj [MAX] [MAX] ; //相邻节点的矩阵
int curr [MAX]; //它给出当前索引来检索节点的
int n = 100;
void read_file(FILE *);
int main()
{

FILE * pf;
int i,r;

if((pf = fopen(" nick.txt"," r"))) == NULL){
printf(错误打开nick.txt文件);
返回1;
}

read_file(pf); //创建矩阵的函数Adj [] []

next_neighbour(1);
返回0;
}

void read_file(FILE * fd)

char buffer [500];

int t;
if(!fscanf(fd,"%d \ n) ;,& n))


以上行将''n''的值更改为您文件中的第一个值
(即0.)

printf(读取文件时出错);


从未输入下面的循环,因为此时''n''等于0.

for(int i = 1; i< = n ; i ++){
fgets(buffer,sizeof(buffer),fd);

t = 0;
for(int j = 0; j< n; j ++){
printf("%d",buffer [j] - ''0'');

if((buffer [j] - ''0'')== 1 )

{
[* t] = j + 1;

printf("%d",Adj [i] [ j]);
//cout<<Ad[i][j].....but nothin works :(

}

}
// printf(" \ n");
// printf(" \ n");

curr [i] = 1;
}

fclose(fd);

}
----------------------- ------------------
我文件的内容如下:
nick.txt:
0 3 -1 3 - 1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
------------- -------------------------------------
我希望这个输入存储在一个内部数组....我怎么能读取这个
文件并将其存储在数组Adj ??????任何帮助将非常赞赏...
问候。
Hi all,
i m trying to read from a file and then copy it into an array...my
code is as follow..it runs fine but i cant understand y it doesnt show
me any output??
here is my code...
using namespace std;
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the
neigbours of a node
int n=100;
void read_file(FILE* );
int main()
{
FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}

read_file(pf); //function to create the matrix Adj[][]

next_neighbour(1);
return 0;
}

void read_file(FILE *fd)
{

char buffer[500];

int t;
if (!fscanf (fd,"%d\n",&n))
The above line changes the value of ''n'' to whatever the first value in
your file is (ie 0.)
printf("error reading the file");
Your loop below is never entered because at this point, ''n'' equals 0.
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);

t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-''0'');

if((buffer[j]-''0'')== 1)

{
Adj[i][++t]= j+1;

printf(" %d ", Adj[i][j]);
//cout<<Ad[i][j].....but nothin works :(

}

}
//printf("\n");
//printf("\n");

curr[i]= 1;
}

fclose(fd);

}
-----------------------------------------
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
--------------------------------------------------
i want this input to b stored inside an array....how can i read this
file and store it inside array Adj??????any help will b greatly
appreciated...
regards.




-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验进行校正。



--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


但仍然......在makin那些变化之后......现在我试着打印
打印... ...打印一些废话.. :((...这里是代码......:

int main()

{


FILE * pf;

int i,r;


if((pf = fopen(" nick.txt", " r"))== NULL){

printf(错误打开nick.txt文件);

返回1;

}


read_file(pf); //创建矩阵的函数Adj [] []


// next_neighbour(1);

返回0;

}


void read_file(FILE * fd)

{


char buffer [500];

int c;

int t;

if(!fscanf (fd,%d \ n,& c))

printf(读取文件时出错);

// do {

//} while(c!= EOF); * /

for(int i = 1; i< = n; i ++){

fgets(缓冲区,sizeof(buff ()),(bd = 0) j< n; j ++){

// printf("%d",buffer [j] - ''0'');


if((buffer [j] - ''0'')== 1)


{

Adj [i] [++ t] = j +1;


// printf("%d",Adj [i] [j]);


}


}

// printf(" \ n");

// printf(" \ n") ;


curr [i] = 1;

}

for(int i = 0; i< n; i ++)

for(int j = 0; j< n; j ++)

cout<< Adj [i] [j];

fclose (fd);


}


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

现在输出为:

C:\Documents和

设置\shuch \cbproject \readFile \windows \ Debug_Bu ild\readFile.exe

00000000000491215000000811141700000021316000000041 31600000002500000000258111800000258111600000581116 0000004111600000049121500000081114170000002131600 000004131600000002500000000258111800000258111 60000 05811160000005811160000005811161111108111417000000 21316000000041316000000025000000002581118000002581 11600000581116000000581116000000581116111111111111 11102131600000004131600000002500000000258111800000 25811160000058111600000058111600000058111611111111 11111111111111111041316000000025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111102581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111111111111110 25811160000058111600000058111600000058111611111111 11111111111111111111111111111111111111111111111111 11111110581116000000581116000000581116111111111111 11111111111111111111111111111111111111111111111111 111111111111105811160000005

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

这是SOOO RANDOM ......我应该做什么?????
but still...after makin those changes..now when i m tryin to
print...its printin some nonsense..:((...here is the code again...:
int main()
{

FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}

read_file(pf); //function to create the matrix Adj[][]

//next_neighbour(1);
return 0;
}

void read_file(FILE *fd)
{

char buffer[500];
int c;
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
// do{
//}while (c != EOF);*/
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);

t=0;
for (int j=0; j<n;j++){
// printf(" %d ", buffer[j]-''0'');

if((buffer[j]-''0'')== 1)

{
Adj[i][++t]= j+1;

//printf(" %d ", Adj[i][j]);

}

}
//printf("\n");
//printf("\n");

curr[i]= 1;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<Adj[i][j];
fclose(fd);

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 31600000002500000000258111800000258111600000581116 00000058111600000049121500000081114170000002131600 00000413160000000250000000025811180000025811160000 05811160000005811160000005811161111108111417000000 21316000000041316000000025000000002581118000002581 11600000581116000000581116000000581116111111111111 11102131600000004131600000002500000000258111800000 25811160000058111600000058111600000058111611111111 11111111111111111041316000000025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111102581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111111111111110 25811160000058111600000058111600000058111611111111 11111111111111111111111111111111111111111111111111 11111110581116000000581116000000581116111111111111 11111111111111111111111111111111111111111111111111 111111111111105811160000005
------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????


文章< 11 ********************** @ j33g2000cwa.googlegroups。 com>,

" Shuch" <嘘********** @ gmail.com>写道:
In article <11**********************@j33g2000cwa.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
但仍然......在makin那些变化之后......现在我试着打印...它打印一些废话.. :((...这里是代码......:
int main()
{

文件* pf;
int i,r;
如果((PF =的fopen(QUOT; nick.txt"," R"))== NULL){
的printf(QUOT;错误打开nick.txt文件");
返回1;
}

read_file(pf); //创建矩阵的函数Adj [] []

// next_neighbour(1);
返回0;
}

void read_file(FILE * fd)
{char buffer [500];
int c;
INT吨;
如果(的fscanf(FD,"!%d\\\
",&安培; c))的
的printf(QUOT;错误读取文件");
/ / do {
//} while(c!= EOF); * /
for(int i = 1; i< = n; i ++){
fgets(buffer,sizeof(buffer) ),fd);

t = 0;
for(int j = 0; j< n; j ++){
// printf(" %d",buffer [j] - ''0'');

if((buffer [j] - ''0'')== 1)
{
Adj [i] [++ t] = j + 1;

// printf("%d",Adj [i] [j]);

}

// printf(" \ n");
// printf(" \ n"); for(int j = 0; j< n; j ++)
cout<< Adj [i] [j];

fclose(fd);

}

- -----------------------------------
现在输出结果为:
C:\Documents和
设置\shuch \cbproject \readFile \windows \ Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 3160000000250000000025811180
00002581116000005811160000005811160000004912150000 008111417000000213160 0000004
13160000000250000000025811180000025811160000058111 6000000581116000000581116111
11081114170000002131600000004131600000002500000000 2581118000002581116000005811
16000000581116000000581116111111111111111021316000 0000413160000000250000000025
81118000002581116000005811160000005811160000005811 1611111111111111111111111110
41316000000025000000002581118000002581116000005811 1600000058111600000058111611
11111111111111111111111111111111102500000000258111 8000002581116000005811160000
00581116000000581116111111111111111111111111111111 1111111111111110258111800000
25811160000058111600000058111600000058111611111111 1111111111111111111111111111
11111111111111111110258111600000581116000000581116 1100001111111111111111111111111111111111111111111110 5811160000005811160000005811
16111111111111111111111111111111111111111111111111 1111111111111111111111111110
5811160000005
--------- -------------------------------------------------- -------
这就是SOOO RANDOM ......我应该做什么??????
but still...after makin those changes..now when i m tryin to
print...its printin some nonsense..:((...here is the code again...:
int main()
{

FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}

read_file(pf); //function to create the matrix Adj[][]

//next_neighbour(1);
return 0;
}

void read_file(FILE *fd)
{

char buffer[500];
int c;
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
// do{
//}while (c != EOF);*/
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);

t=0;
for (int j=0; j<n;j++){
// printf(" %d ", buffer[j]-''0'');

if((buffer[j]-''0'')== 1)

{
Adj[i][++t]= j+1;

//printf(" %d ", Adj[i][j]);

}

}
//printf("\n");
//printf("\n");

curr[i]= 1;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<Adj[i][j];
fclose(fd);

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 3160000000250000000025811180
00002581116000005811160000005811160000004912150000 0081114170000002131600000004
13160000000250000000025811180000025811160000058111 6000000581116000000581116111
11081114170000002131600000004131600000002500000000 2581118000002581116000005811
16000000581116000000581116111111111111111021316000 0000413160000000250000000025
81118000002581116000005811160000005811160000005811 1611111111111111111111111110
41316000000025000000002581118000002581116000005811 1600000058111600000058111611
11111111111111111111111111111111102500000000258111 8000002581116000005811160000
00581116000000581116111111111111111111111111111111 1111111111111110258111800000
25811160000058111600000058111600000058111611111111 1111111111111111111111111111
11111111111111111110258111600000581116000000581116 0000005811161111111111111111
11111111111111111111111111111111111111111111111110 5811160000005811160000005811
16111111111111111111111111111111111111111111111111 1111111111111111111111111110
5811160000005
------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????




首先,你应该超越实际代码而不是你上面的代码。

其次,你应该考虑在C新闻组中发布该代码,而不是C ++新闻组的



你想做什么?不完全是,但是在抽象意义上。

这是代码必须阅读的唯一输入文件吗?如果没有,

其他文件是否都具有相同的宽度和高度?如果没有,他们会不会是
都是正方形?


坦率地说,解决问题的最简单方法就是:


const int Adj [7] [7] = {{0,3,-1,3,-1,-1,-1},

{3,0,2, -1,-1,-1,-1},

{-1,2,0,2,2,-1,-1},

{3 ,-1,2,0,6,-1,-1},

{-1,-1,2,6,0,4,3},

{-1,-1,-1,-1,4,0​​,-1},

{-1,-1,-1,-1,3,-1,0} };


int main()

{

for(int i = 0; i< 7; ++ i )(* j = 0; j <7; ++ j)

cout<< Adj [i] [j]<< " " ;;

cout<< ''\ n'';

}

}


-

魔法取决于关于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验进行校正。



First, you should past the actual code rather than what you have above.
Second, you should consider posting that code in a C newsgroup instead
of a C++ newsgroup.

What is it you are trying to do? Not exactly, but in an abstract sense.
Is this the only input file the code will ever have to read? If not,
will other files all have the same width and height? If not, will they
all be squares?

Frankly, the easiest solution to your problem would be something like:

const int Adj[7][7] = { { 0, 3, -1, 3, -1, -1, -1 },
{ 3, 0, 2, -1, -1, -1, -1 },
{ -1, 2, 0, 2, 2, -1, -1 },
{ 3, -1, 2, 0, 6, -1, -1 },
{ -1, -1, 2, 6, 0, 4, 3 },
{ -1, -1, -1, -1, 4, 0, -1 },
{ -1, -1, -1, -1, 3, -1, 0 } };

int main()
{
for ( int i = 0; i < 7; ++i ) {
for ( int j = 0; j < 7; ++j )
cout << Adj[i][j] << " ";
cout << ''\n'';
}
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


这篇关于读取文件并将其复制到数组中......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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