C 反转文件中的行顺序
C Reverse lines order from files
我被要求制作一个程序来反转文件的行顺序作为输入。示例:
$猫文件1
一月
二月
行军
$猫文件2
一
二
三
$ ./程序文件1 文件2
三
二
一
行进
二月
一月
所以,几乎所有的东西我都做了,反转也很好,但是问题来了。该程序打印第一个引入的文件,而不是最后一个文件。示例:
$ ./程序文件1 文件2
行进
二月
一月
三
二
一个
我已经尝试了所有方法,但仍然无法弄清楚问题出在哪里。
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "auxiliar.h"
#define MAXLINEA 2048
#define Bocabajo "bocabajo"
void Ayuda(void){
fprintf(stdout, "%s: Uso: %s [ fichero... ]\n", Bocabajo, Bocabajo);
fprintf(stdout, "%s: Invierte el orden de las lineas de los ficheros (o de la entrada).\n", Bocabajo);
exit(EX_OK);
}
int main ( int argc, char *argv[] )
{
char linea[MAXLINEA];
char **lineas;
int tamanio = 0;
int nleidos = 0;
int i,j;
FILE *file;
if (argc == 2 && !strcmp(argv[1], "-h")){
Ayuda();
}
else if (argc == 1) {
tamanio = 2;
lineas = malloc(sizeof(char *));
while (fgets (linea, MAXLINEA, stdin) ) {
if (nleidos >= tamanio) {
tamanio *= 2;
lineas = realloc (lineas, sizeof(char *) * tamanio);
if (!lineas)
Error(EX_OSERR, " Ubicacion memoria dinamica desconocida");
}
lineas[nleidos] = strdup(linea);
nleidos++;
}
for ( i=0; i<nleidos; i++)
printf ("%s", lineas[nleidos-i-1]);
for ( i=0; i<nleidos; i++)
free (lineas[i]);
free (lineas);
}else{
for (i=1; i<argc; i++){
tamanio = 2;
lineas = malloc(sizeof (char *));
nleidos = 0;
if ((file = fopen(argv[i], "r")) == NULL){
Error(EX_NOINPUT,".*\"%s\" ilegible o inexistente",argv[i]);
}else{
while (fgets (linea, MAXLINEA, file) != NULL){
if (nleidos >= tamanio) {
tamanio *= 2;
lineas = realloc (lineas, sizeof(char *) * tamanio);
if (!lineas)
Error(EX_OSERR, " Ubicacion memoria dinamica desconcida");
}
lineas[nleidos] = strdup(linea);
nleidos++;
}
fclose(file);
for (j=0; j<nleidos; j++)
printf ("%s", lineas[nleidos-j-1]);
for (j=0; j<nleidos; j++)
free (lineas[j]);
free(lineas);
}
}
}
return(EX_OK);
}
PD:抱歉我的英语不太好。
以相反的方向迭代 command-line 参数。改变这个:
for (i=1; i<argc; i++){
对此:
for (i=argc-1; i>0; i--){
我被要求制作一个程序来反转文件的行顺序作为输入。示例:
$猫文件1 一月 二月 行军
$猫文件2 一 二 三
$ ./程序文件1 文件2 三 二 一 行进 二月 一月
所以,几乎所有的东西我都做了,反转也很好,但是问题来了。该程序打印第一个引入的文件,而不是最后一个文件。示例:
$ ./程序文件1 文件2 行进 二月 一月 三 二 一个
我已经尝试了所有方法,但仍然无法弄清楚问题出在哪里。 这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "auxiliar.h"
#define MAXLINEA 2048
#define Bocabajo "bocabajo"
void Ayuda(void){
fprintf(stdout, "%s: Uso: %s [ fichero... ]\n", Bocabajo, Bocabajo);
fprintf(stdout, "%s: Invierte el orden de las lineas de los ficheros (o de la entrada).\n", Bocabajo);
exit(EX_OK);
}
int main ( int argc, char *argv[] )
{
char linea[MAXLINEA];
char **lineas;
int tamanio = 0;
int nleidos = 0;
int i,j;
FILE *file;
if (argc == 2 && !strcmp(argv[1], "-h")){
Ayuda();
}
else if (argc == 1) {
tamanio = 2;
lineas = malloc(sizeof(char *));
while (fgets (linea, MAXLINEA, stdin) ) {
if (nleidos >= tamanio) {
tamanio *= 2;
lineas = realloc (lineas, sizeof(char *) * tamanio);
if (!lineas)
Error(EX_OSERR, " Ubicacion memoria dinamica desconocida");
}
lineas[nleidos] = strdup(linea);
nleidos++;
}
for ( i=0; i<nleidos; i++)
printf ("%s", lineas[nleidos-i-1]);
for ( i=0; i<nleidos; i++)
free (lineas[i]);
free (lineas);
}else{
for (i=1; i<argc; i++){
tamanio = 2;
lineas = malloc(sizeof (char *));
nleidos = 0;
if ((file = fopen(argv[i], "r")) == NULL){
Error(EX_NOINPUT,".*\"%s\" ilegible o inexistente",argv[i]);
}else{
while (fgets (linea, MAXLINEA, file) != NULL){
if (nleidos >= tamanio) {
tamanio *= 2;
lineas = realloc (lineas, sizeof(char *) * tamanio);
if (!lineas)
Error(EX_OSERR, " Ubicacion memoria dinamica desconcida");
}
lineas[nleidos] = strdup(linea);
nleidos++;
}
fclose(file);
for (j=0; j<nleidos; j++)
printf ("%s", lineas[nleidos-j-1]);
for (j=0; j<nleidos; j++)
free (lineas[j]);
free(lineas);
}
}
}
return(EX_OK);
}
PD:抱歉我的英语不太好。
以相反的方向迭代 command-line 参数。改变这个:
for (i=1; i<argc; i++){
对此:
for (i=argc-1; i>0; i--){