Home > Blockchain >  What do I have to change in my code to get the right result?
What do I have to change in my code to get the right result?

Time:11-28

How can I get the result what I want?

My code reverse a file and every line of the file and print a number in front of the lines.But in my code this is not working well. Example:

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

My problem is that I get this result:

There is just a 3 number but I need 1 and 2 also.

3 avlizs
kcarab
amla

What do I have to change in my code to work well?

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

CodePudding user response:

I guess your text file was produced on Windows with \r\n line-endings and your program runs on UNIX. If this is the case, the fopen(..., "r") call won't convert these line-endings to \n (it would have been done on Windows) and when you strip the last \n of the line, the \r stays at the end. Displaying such a line will jump to the left margin and overwrite the previously written number.

I suggest this stripping, just to be certain

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

but it's just guessing...

  • Related