将一个int数组的一半写入一个新的文本文件中。

问题描述 投票:1回答:2

在我的程序中,我试图打开命令行提供的文本文件,然后创建两个新的文本文件 values1.txt 和 values2.txt。命令行提供的文件包含100个唯一的整数。一半的数字被复制到values1.txt中,另一半被复制到values2.txt中。

然而,当我试图对values1.txt进行操作时,我得到了下面的值。

101
32758
0
0
176197744
32764
3
0
176197728
32764
0
0
-78325960
32758
0
0
1
0
-78326000
32758
0
0
1700966438
0
-78325096
32758
176197896
32764
176197952
32764
-78326000
32758
0
0
-80547345
32758
0
0
176197952
32764
0
0
0
0
0
0
-78326000
32758
-82942073
32758
8

这是我的代码

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

int cfileexists(const char * filename){
    /* try to open file to read */
    FILE *file;
    if (file = fopen(filename, "r")){
        fclose(file);
            return 1;
    }   
} 

int main(int argc,char*argv[]){
    char *p= argv[1];
    int numArray[100];
    int i=0;
    int sum1arr[50];
    int sum2arr[50];

    if (! cfileexists(p)){
        printf("Error, unable to locate the data file %s\n", p);
        return 100;
    }
    for(i;i<100;i++){
        FILE *stream=fopen(p,"r");
        while(!feof(stream)){

        fscanf(stream,"%d",&numArray[i]);

        }
        FILE * fPtr = NULL;
        fPtr = fopen("values1.txt", "w");
        for(int j=0;j<50;j++){
            fprintf(fPtr,"%d\n",numArray[j]);
        }
    }
}

为了便于比较,这是源文本文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
c arrays file file-io concurrency
2个回答
1
投票

脚本中,我想打开命令行提供的文本文件,然后创建两个新的文本文件values1.txt和values2.txt。for 语句有太多的循环,你应该使用读循环也写,你也可以使用fscanf return来验证文件何时到达终点。如果你不需要保存值,你甚至可以使用singe int 来暂时存储该值,直到它被写入。

这里是一个可能的实现,并附有注释。

现场演示

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

int cfileexists(const char *filename) 
{
    /* try to open file to read */ 
    FILE *file;
    if ((file = fopen(filename, "r")))
    {
        fclose(file);
        return 1;
    }   
    return 0; //return 0 if file not found
}

int main(int argc, char *argv[])
{   
    if(argc != 2){      //check number of arguments
        puts("Wrong number of arguments");
        return(EXIT_FAILURE);  //EXIT_FAILURE macro is more portable
    }

    FILE *stream;    
    FILE *fPtr = NULL;
    FILE *fPtr2 = NULL;

    int numArray[100];
    int i = 0;

    fPtr = fopen("values1.txt", "w");
    fPtr2 = fopen("values2.txt", "w");

    //you can use fopen to check file nstead of the 
    //function, it returns null if no file is opened
    if (!(stream = fopen(argv[1], "r")))
    {
        perror("Error, unable to locate the data file"); //also prints error signature
        return EXIT_FAILURE;
    }
    //check input, stop when there is nothing else to read
    while (fscanf(stream, "%d", &numArray[i]) == 1 && i < 100) 
    {
        if(i < 50)
            fprintf(fPtr, "%d\n", numArray[i]); //write to file 1
        else
            fprintf(fPtr2, "%d\n", numArray[i]); //write to file 2
        i++;
    }
    fclose(stream);
    fclose(fPtr);
    fclose(fPtr2);
}

0
投票

这里有一个不同的方法,使用 getline():

#include <stdio.h>
#include <stdlib.h>

#define SPLIT_POINT 50

int main(void) {
    char filename[] = "test";
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        fprintf(stderr, "Couldn't open %s\n", filename);
    }

    // Check file open (as above) left out for brevity
    FILE *out1 = fopen("values1.txt", "w");
    FILE *out2 = fopen("values2.txt", "w");
    char *line = NULL;
    size_t n = 0, i = 0;
    ssize_t len = 0;
    while ((len = getline(&line, &n, fp)) != EOF) {
        // Select which output file to write to
        FILE *out = i < SPLIT_POINT ? out1 : out2;

        // getline includes the newline - remove this
        if (i == SPLIT_POINT - 1 && len > 0) {
            line[--len] = 0;
        }
        // Write to the relevant output file
        fprintf(out, "%s", line);
        i++;
    }
    fclose(fp);
    fclose(out1);
    fclose(out2);
    free(line);
    return 0;
}

如果你不在乎删除新行,你可以简化以下内容 getline() 呼叫--只是不要打扰 len 可变的。

© www.soinside.com 2019 - 2024. All rights reserved.