【问题标题】:What do I have to change in my code to get the right result?我必须在代码中进行哪些更改才能获得正确的结果?
【发布时间】:2022-01-05 06:40:57
【问题描述】:

我怎样才能得到我想要的结果?

我的代码反转一个文件和文件的每一行,并在行前打印一个数字。但在我的代码中,这不起作用。 示例:

test.txt :
alma
barack
szilva
Result:
3 avlizs
2 kcarab
1 amla

我的问题是我得到了这个结果:

只有 3 个数字,但我还需要 1 和 2。

3 avlizs
kcarab
amla

我必须对我的代码进行哪些更改才能正常工作?

#ifndef REVERSE_H
#define REVERSE_H

void reverse( FILE *fp, char *name);

#endif /* REVERSE_H */ 
#include <stdio.h>
#include "reverse.h"

int main( int argc, char *argv[])
{
  if ( argc < 2 )
  {
    reverse( stdin, "stdin"); /* stdin */     
  }
  else
  {
    int i;
    for ( i = 1; i < argc; ++i )  /* fájlnevek */     
    {
      FILE *fp = fopen( argv[i], "r");
      if ( NULL != fp )
      {
         reverse( fp, argv[i]); /* fp-ről olvas */
         fclose(fp);
      }
      else
      {
        fprintf(stderr,"Can't open %s for read\n",argv[i]);       
      }
    }
  }
  return 0;
}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reverse.h"

#define INITCAP 4     
#define BUFSIZE 1024  

char **read( FILE *fp, int *pLines);
void   write( char **lines, int nLines, char *name);
void   revline( char *line);
void   memoria( void *ptr);

void reverse( FILE *fp, char *name)
{
  int nLines = 0;
  char **lines = read( fp, &nLines);    
  write( lines, nLines, name); 
}

     
char **read( FILE *fp, int *pLines)
{
  int  capacity = INITCAP;   
  int  nLines   = 0;         
  char buffer[BUFSIZE]; 
 
  char **lines = (char **)malloc(capacity * sizeof(char*));
  memoria(lines);
  while ( NULL != fgets(buffer, BUFSIZE, fp) ) 
  {
    int len = strlen(buffer);    
    if ( '\n' == buffer[len-1] )  
    {
      buffer[len-1] = '\0';     
    }
    if ( nLines == capacity )   
    {
      capacity *= 2;         
      lines = realloc( lines, capacity * sizeof(char *));
      memoria(lines);
    }
    lines[nLines] = (char *) malloc(len+1);    
    memoria(lines[nLines]);
    strcpy( lines[nLines], buffer);     
    ++nLines;
  }
  *pLines = nLines;  
  return lines;
}

void   write( char **lines, int nLines, char *name)
{
  
  for (int i = nLines-1; i >= 0; --i) 
  {
    char *curr = lines[i];
    fprintf( stdout, " %d " ,  i+1 );
    revline(curr); 
    free( curr);
  }
  free(lines);
}

void revline( char *line)
{
  int len = strlen(line);     
  while ( --len >= 0 )
  {
    fputc( line[len], stdout);      
  }
  fputc( '\n', stdout);
}

void memoria( void *ptr)
{
  if ( 0 == ptr )
  {
    fprintf( stderr, "Memory allocation failed!\n");
    exit(1);    
  }   
}

【问题讨论】:

  • 我无法确认问题,输出符合预期。也许重新编译?
  • 我试过了,但在第一行的输出中,它在单词前面写了一个数字,但在接下来的几行中,它只写了单词。
  • 也不能复制,即使输入文件不以换行符结尾。

标签: c output reverse


【解决方案1】:

您的文本文件是在 Windows 上生成的,带有 \r\n 行结尾,并且您的程序在 UNIX 上运行。 如果是这种情况,fopen(..., "r") 调用不会将这些行尾转换为 \n(它本来可以在 Windows 上完成)并且当您剥离最后一行 \n 时,\r 将保留在末尾。 显示这样的一行会跳转到左边距并覆盖之前写入的数字。

我建议这种剥离,只是为了确定

    while ( len && ('\n' == buffer[len-1] || '\r' == buffer[len-1]) )  
    {
      buffer[--len] = '\0';
    }

但这只是猜测......

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 2016-06-14
    • 2011-12-04
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多