I'm having a weird bug where the text I append is being added to the beginning of a file instead of the end of it. This ends up making everything backwards.
CODE:
#include <stdio.h>
#include <stdlib.h>
//Check Values
void checkValues(int numDisks, int raidType, int chunkSize){
while (numDisks < 1 || numDisks > 9){
printf("Number inputed for numDisks is not valid, please enter a new value (1-9).\n");
scanf("%d", &numDisks);
}
while(raidType < 0 || raidType > 1){
printf("Number inputed for raidType is not valid, please enter a new value (0-1).\n");
scanf("%d", &raidType);
}
while(chunkSize < 1 || chunkSize > 512){
printf("Number inputed for chunkSize is not valid, please enter a new value (1-512).\n");
scanf("%d", &chunkSize);
}
}
//CreateFile
void createFile(int numDisks){
char raidName[5];
int counter = 1;
//Create each file
while(counter != numDisks + 1){
sprintf(raidName, "raid%d", counter); //Append counter to string
FILE *out = fopen(raidName, "w");
counter++;
}
}
//Write File
void writeFile(int numDisks, char *buffer, int counter){
char raidName[5];
sprintf(raidName, "raid%d", counter); //Append counter to string
//Write File
FILE *out = fopen(raidName, "a");
fprintf(out, "%s", buffer);
}
//Read and Write Files
void rwFile(const char *fileName, int chunkSize, int numDisks){
char buffer[10000];
int counter = 1;
//Reading File
FILE *in = fopen(fileName, "r");
if(in == NULL) return;
//Create File
createFile(numDisks);
while(fgets(buffer, chunkSize + 1, in) != NULL){
//Reset Counter
if(counter > numDisks){
counter = 1;
}
writeFile(numDisks, buffer, counter);
counter++;
}
}
int main(int argc, const char *argv[]){
//Declarations
const char *fileName = argv[1];
int numDisks = atoi(argv[2]);
int raidType = atoi(argv[3]);
int chunkSize = atoi(argv[4]);
checkValues(numDisks, raidType, chunkSize);
rwFile(fileName, chunkSize, numDisks);
}
raid1.txt:
mmmmiiiieeeeaaaa
What should be written to raid1.txt:
aaaaeeeeiiiimmmm
I have tried using fseek but to no avail. If anybody could help me out or point me in the right direction that would help. This is done in C using bash to compile and run the program. The arguments that are passed are test.txt 4 0 4.
I imagine the problem you are seeing is due to each call to writeFile
is opening a fresh handle to the file, writing to it and then just returning. Once you have finished using a file handle you should close it, this will flush the file buffer to the file and free up the handle.
What I think is happening here is you are opening up a fresh handle each time and each of these is only having a small amount of data written to it, so isn't being flushed to disk. You program is then exiting and the OS is cleaning up the stale file handles flushing them to disk. As a guess I imagine it is doing that clean up in reverse order hence the appearance of your file being written backwards.
Update your writeFile
to include a close and hopefully that should resolve the issue
FILE *out = fopen(raidName, "a");
fprintf(out, "%s", buffer);
fclose(out);
It might be nicer if you open the file for writing in the loop calling writeFile
and pass the handle over, and then you only need to open it and close it once.
Worth adding closes elsewhere, where you have opened files too.
HTH
我看到的另一件事是您在检查值(k)中将局部变量的地址用于scanf,它们仅是局部的,永远不会反映在Main()中
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.