我有一個名為 as 的輸入檔案datafile.data,如下所示:
1,2,1,1,0
1,3,1,1,0
1,1,2,2,1
2,1,2,2,1
2,3,2,3,1
1,1,2,3,2
3,1,1,4,2
2,1,3,2,2
3,3,3,1,2
2,2,3,4,2
這里的第 4 列代表 4 個屬性值,比如 A1、A2、A3、A4。最后一列代表類值。對于這個特定的示例檔案,有 4 個屬性,但對于其他一些檔案,可以有“n”個屬性,但對于每個檔案,最后一列將給出類值。
現在我想將此檔案轉換為另一個名為的檔案: outputfile.exp
輸出檔案的第一行如下所示:
<Number of rows in the .data file> <Number of attributes> <Max value of A1> <Max value of A2> <Max value of A3> <Max value of A4> <(Max value of last column) 1>
并且輸出檔案的其余行將與資料檔案相同,只有一個更改,即最后一列的每個值將增加 1。
例如,上述示例的輸出檔案將如下所示:
10 4 3 3 3 4 3
1,2,1,1,1
1,3,1,1,1
1,1,2,2,2
2,1,2,2,2
2,3,2,3,2
1,1,2,3,3
3,1,1,4,3
2,1,3,2,3
3,3,3,1,3
2,2,3,4,3
其中第一行的 10 是行數,4 是存在的屬性數,(3,3,3,4) 這 4 是屬性 A1,A2,A3 和 A4 的最大值,最后 3 代表最高等級值 1。最后一列的每個值也增加了 1。
下面我附上我的嘗試:
#include <stdio.h>
#include <string.h>
#define MAX_FILE_NAME 100
int main()
{
FILE *fp;
int count = 0; // Line counter (result)
char filename[MAX_FILE_NAME], dataToBeRead[50];
char c; // To store a character read from file
// Open the file
fp = fopen("datafile.data", "r");
// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count 1;
fclose(fp);
printf("%d\n",count);
fp = fopen("datafile.data", "r");
if ( fp == NULL )
{
printf( "Failed to open." ) ;
}
else
{
while( fgets ( dataToBeRead, 50, fp ) != NULL )
{
printf( "%s" , dataToBeRead ) ;
}
fclose(fp) ;
}
return 0;
}
我得到以下輸出:
10
1,2,1,1,1
1,3,1,1,1
1,1,2,2,2
2,1,2,2,2
2,3,2,3,2
1,1,2,3,3
3,1,1,4,3
2,1,3,2,3
3,3,3,1,3
2,2,3,4,3
現在我無法進一步進行,因為我對 C 很陌生,請幫助我。
uj5u.com熱心網友回復:
您真的不想這樣做,因為倒帶輸入流是一種反模式。但是您可以執行以下操作:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * xfopen(const char *path, const char *mode);
void * xmalloc(size_t s);
void
parse_line(const char *buf, int *max, int column_count)
{
for(int i = 0; i < column_count; i ){
char *end;
int t = strtol(buf, &end, 10);
if( t > max[i] ){
max[i] = t;
}
if( !((i < column_count - 1 && *end == ',')
|| (i == column_count - 1 && *end == '\n')) ){
fprintf(stderr, "invalid input '%c' in %s", *end, buf);
exit(1);
}
buf = end 1;
}
}
int
main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "stdin";
FILE *in = argc > 1 ? xfopen(path, "r") : stdin;
char buf[1024];
int column_count = 1;
int row_count = 1;
int *max;
/* Read first line to determine number of columns */
if( fgets(buf, sizeof buf, in) == NULL ){
fputs("Input error\n", stderr);
return 1;
}
for( const char *p = buf; *p; p ){
if( *p == ',' ){
column_count = 1;
}
}
max = xmalloc(column_count * sizeof *max);
for( int i = 0; i < column_count; i ){
max[i] = INT_MIN;
}
parse_line(buf, max, column_count);
while( fgets(buf, sizeof buf, in) != NULL ){
row_count = 1;
parse_line(buf, max, column_count);
}
if( fseek(in, 0L, SEEK_SET) ){
perror(path);
return 1;
}
printf("%d %d ", row_count, column_count - 1);
for( int i = 0; i < column_count - 1; i = 1 ){
printf("%d ", max[i]);
}
printf("%d\n", max[column_count - 1] 1);
while( fgets(buf, sizeof buf, in) != NULL ){
char *comma = strrchr(buf, ',');
if( comma == NULL ){
fprintf(stderr, "Invalid input\n");
return 1;
}
*comma = '\0';
int k = strtol(comma 1, NULL, 10);
printf("%s,%d\n", buf, k 1);
}
}
FILE *
xfopen(const char *path, const char *mode)
{
FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path, mode) :
*mode == 'r' ? stdin : stdout;
if( fp == NULL ){
perror(path);
exit(EXIT_FAILURE);
}
return fp;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ){
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}
您可以執行此為./a.out < datafile.data > outputfile.exp或./a.out datafile.data > outputfile.exp,但是這會不會,如果你嘗試從管道讀取(作業seek會失敗)。該seek故障并無法運行這是一個過濾網,使這個次優的方法,但在記憶體中存盤整個檔案也有缺點。
uj5u.com熱心網友回復:
由于威廉珀塞爾在 C 中提供了極好的答案,這里有一個awk替代方案,盡管awk沒有標記。
awk -F, -v OFS="," ' # assign input/output field separator to a comma
NR==FNR { # this block is invoked for the 1st read of the input file
for (i = 1; i <= NF; i ) { # loop over the filelds
if (max[i] == "" || max[i] < $i) max[i] = $i
# update the max values
}
nr = NR; nf = NF # store #records and #fields
next # skip following statements
}
FNR==1 { # this block is invoked just before reading he 1st line for the 2nd read of the input file
printf("%d %d ", nr, nf - 1) # print #records and #fields - 1
max[nf] # increment the max value of the last field
for (i = 1; i <= nf; i ) { # print max values
printf("%d%s", max[i], i==nf ? "\n" : " ");
}
}
{ # this block is invoked for the 2nd read
$nf # increment the value of the last field
print # print fields as csv
}
' datafile.data datafile.data # read the input file twice
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344658.html
