我的编码有什么问题?有人弯腰.. [英] whats wrong with my coding? someone plis..

查看:82
本文介绍了我的编码有什么问题?有人弯腰..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 /*  假设我有一个名为word.txt的文件,其中包含100个单词*/
#include   ><  > 
 #include   <   stdlib.h  > 
  struct 列表// 结构函数
{
字符 word [ 20 ];
};

 int  main()
{
结构列表n1 [ 100 ];
字符网格[ 15 ] [ 15 ] ;

FILE * inp,// 用于加密输入文件的指针.
* outp; // 用于加密输出文件的指针.

 int  k =  0 ,j =  0  ,i =  0 ,m =  0 ;

/*  打开文件并读取矩阵编号和名称列表*/
inp = fopen(" "  r");
outp = fopen(" "  w");

如果(inp == NULL)// 如果输入为没事
{
              printf(" );
}
          其他
{
               for (k =  0 ; k <100; k ++)
              {
                  fscanf(inp," ,n1 [k] .word);
                   for (i =  0 ; i< 15; i ++)
                  {
                       for (j =  0 ; j< 15; j ++)
                      {
                          grid [i] [j] = n1 [k] .word [m];
                          m ++;
                          如果(n1 [k] .word [m] == '  break ;
                      }
                      m =  0 ;
                       break ;
                 }
            }
             for (i =  0 ; i< 15; i ++)
            {
                 for (j; j< 15; j ++)
                {
                    grid [i] [j] = rand();
                }
            }
             for (i =  0 ; i< 15; i ++)
            {
                 for (j =  0 ; j< 15; j ++)
                {
                    fprintf(outp," ,网格[i] [j] );
                }
                fprintf(outp," );
            }
        }
        系统(" );
        返回  0 ;
    } 

解决方案

在阅读您的注释之后,听起来好像您的程序只是被卡住了(相对于未编译/链接).如果是这种情况,请使用调试器来帮助您确定卡住的位置(可能是永不返回的调用,也可能是由不良逻辑引起的无限循环).

当您将代码粘贴到CodeProject上时,其代码格式不正确,因此很难正确读取,您可能需要修复该问题以获得更好的帮助.


您的程序正在运行:该行

系统(" ); 

暂停当前任务,因此您可以查看输出(但是以一种非常低效的方式-使用getchar(),并且在调用它之前,请始终单独打印按任意键继续"的行).

只是花了很长时间才执行.
尝试在每个循环之前和之后添加printf语句以跟踪进度-您随时可以将其取出!


您应该在某处检查EOF字符是否在读取文件时停止.

/* let say i have a file named word.txt contains 100 words*/
#include<stdio.h>
#include <stdlib.h>
 struct list			//structure function
{
	char word[20];	
};

int main()
{
	struct list n1[100];
	char grid[15][15];
	
	FILE*inp,                      //pointer for input file of encryption.
		*outp;                     //pointer for  output file for encryption.
		 
	int k=0,j=0,i=0,m=0;

	/*  Open file and read the matric number and the name list  */
	inp=fopen("word.txt","r"); 	
	outp=fopen("grib.txt","w");	

	  if (inp == NULL)   //if input is nothing
	  {
              printf("Error opening input file.  \n");
	  }
          else
	  {
              for(k=0;k<100;k++)	
              {
                  fscanf(inp," %s",n1[k].word);                  
                  for(i=0;i<15;i++)	
                  {
                      for(j=0;j<15;j++)				
                      {
                          grid[i][j]=n1[k].word[m];
                          m++; 
                          if(n1[k].word[m]== '\0')
                              break;
                      }
                      m=0; 
                      break;    
                 }
            }
            for(i=0;i<15;i++)	
            {
                for(j;j<15;j++)					
                {
                    grid[i][j]=rand();  
                }
            }
            for(i=0;i<15;i++)	
            {
                for(j=0;j<15;j++)					
                {
                    fprintf (outp,"%s\t\t\t",  grid[i][j]);  
                }
                fprintf (outp,"\n\n\n");  
            }
        }
        system("pause");
        return 0;
    }

解决方案

After reading your comments, this is starting to sound like your program is just getting stuck (versus not compiling/linking). If that''s the case, use a debugger to help you identify the place where you''re getting stuck (may be a call that never returns or an infinite loop caused by bad logic).

Your code doesn''t have very good formatting when you pasted it onto CodeProject so it''s a little hard to read correctly, you may want to fix that to get better help.


Your program is working: the line

system("pause");

Suspends the current task, so you can look at the output (but in a very inefficient way - use getchar() instead, and always print something alone the lines of "Press any key to continue" before you call it).

It just didn''t take very long to execute.
Try adding printf statements before and after each loop to track the progress - you can always take them out later!


You should somewhere check for EOF character to stop while reading the file.


这篇关于我的编码有什么问题?有人弯腰..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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