Creating and writing in multiple txt files in C - c

I've got a code which creates j files (Node[j].ID) in a directory called (Nodes) and in those j files the code writes the info contained in NodeResults. At the moment the code doesn't neither create nor write in files because of the strcat function doesn't work. Please any idea how to correct the code in order to get the created files with the info contained in NodeResults on it?. Thanks in advance. Please find the code below:
{
int period, j ;
FILE*temporal;
FILE* temp_time;
char path[25];
char* extention = ".txt";
char s[30];
char temporal2[25];
long time_val = 0;
_mkdir("Nodes");
_mkdir("time");
temp_time = fopen("Time/time.txt", "w");
fprintf(temp_time, "%d,%d\n", ReportStep, Nperiods);
fclose(temp_time);
for ( j = 0; j < Nobjects[NODE]; j++ ) {
/* File path writing */
strcpy(temporal2,"Nodes/");
strcat(temporal2, Node[j].ID);
strcat(temporal2, extention);
temporal= fopen(temporal2, "w");
}
for ( period = 1; period <= Nperiods; period++ ) {
output_readNodeResults(period, j);
fprintf(temporal, "%9.3f,%9.3f,%9.3f,%9.3f,%9.3f\n",
NodeResults[NODE_INFLOW],
NodeResults[NODE_OVERFLOW],
NodeResults[NODE_DEPTH],
//NodeResults[NODE_HEAD],
NodeResults[NODE_VOLUME]);
}
fclose(temporal);
return Nperiods;
}

You open a bunch of files in the first for loop, but do not write anything to them. At each iteration, you assign a new FILE * to variable temporal, overwriting any previous value. Afterward, in your second for loop you write a bunch of output to the last file opened -- the one to which temporal refers at that point.
It looks like you want to move the body of the second for loop and the fclose() into the first for loop.

I have properly formatted your code, and now John's comment immediately sticks out: the brace is on the wrong line, resulting in wrong for loops and blocks!
Should you have formatted the code properly yourself, you would have seen it immediately yourself!

Related

Recursive function : abort-condition

We need to create a binary tree which contains content of textfiles. The pointer selection_a and selection_b pointing to another textfile in the directory.
The structure of the textfiles is following:
line: Title
line: OptionA
line: OptionB
line: Text.
The first file is given as parameter while starting the program. All files should be saved at the beginning of the program. Then the text of the first file shows, and the user can input A or B to continue. Based on the selection, the text of File Option A/B is shown and the user can decide again.
The last file of a tree contains no Options: lines 2 and 3 are "-\n".
The problem is, this code only reads all the option A files of the first tree. It doesn't read in any B-Options. In the end, the program shows a memory access error.
I think the problem is that the readingRows function has no abort condition.
current->selection_a = readingRows(input_selection_a);
current->selection_b = readingRows(input_selection_b);
I know the code may be kind of chaotic, but we are beginners in programming. Hope anybody can help us to write an abort-condition.
The function should be aborted if the content of option A (line 3) is "-\n".
Here is the whole function:
struct story_file* readingRows(FILE *current_file)
{
char *buffer = fileSize(current_file);
char *delimiter = "\n";
char *lines = strtok(buffer, delimiter);
int line_counter = 0;
struct story_file *current = malloc(sizeof(struct story_file));
while(lines != NULL)
{
if(line_counter == 0)
{
current->title = lines;
}
else if(line_counter == 1)
{
char *filename_chapter_a = lines;
FILE *input_selection_a = fopen(filename_chapter_a, "r");
if(input_selection_a)
{
current->selection_a = readingRows(input_selection_a);
}
fclose(input_selection_a);
}
else if(line_counter == 2)
{
char *filename_chapter_b = lines;
FILE *input_selection_b = fopen(filename_chapter_b, "r");
if(input_selection_b)
{
current->selection_b = readingRows(input_selection_b);
}
fclose(input_selection_b);
}
else if (line_counter >= 3)
{
current->text = lines;
}
lines = strtok(NULL, delimiter);
line_counter++;
}
return current;
}
There are two items that define a terminating recursive function:
One or more base cases
Recursive calls that move toward a base case
Your code has one base case: while (lines!=NULL) {} return current;, it breaks the while loop when lines is NULL and returns current. In other words, within any particular call to your function, it only terminates when it reaches the end of a file.
Your code moves toward that base case as long as your files do not refer to each other in a loop. We know this because you always read a line, take an action according to your if-else block, and the read the next line. So you always move toward the end of each file you read.
But as you note, the issue is that you don't have a case to handle "no Options", being when lines 2 or 3 are "-\n". So right now, even though you move through files, you are always opening files in line 2. Unless a file is malformed and does not contain a line 2, your recursive call tree never ends. So you just need to add another base case that looks at whether the beginning of lines matches "-\n", and if it does, return before the recursive call. This will end that branch of your recursive tree.
Inside of your while loop, you will need code along the lines of:
if `line_counter` is `2` or `3`
if `lines` starts with your terminating sequence "-\n"
return current
else
`fopen` and make the recursive call
In the parent function that made the recursive call, it will move to the next line and continue as expected.
P.S. Make sure you use free for each malloc you do.

Where am I going wrong in getting this function to do what I would like?

I have written the following function in my C program. The program loads a text file (Les Miserables Vol. I) as well as another text file of 20 of the characters names. The purpose of this function is to scan the entire file, line by line, and count the number of times any of the 20 names appear.
NumOfNames = 20.
Names is an array of the 20 names stored from Names[1] - Names[20].
MaxName is a global integer variable which I would like to store the total number of name appearances throughout the file (It should be in the hundreds or even thousands).
EDIT: After the function is executed, the value of MaxName is 4. I am completely lost as to where I have made a mistake, but it appears that I have made several mistakes throughout the function. One seems to be that it only executed the first iteration of the for loop i.e. it only searches for Name[1], however the first name appears 196 times in the file, so it still isnt even working correctly for just the first name.
void MaxNameAppearances()
{
char LineOfText[85];
char *TempName;
FILE *fpn = fopen(LesMisFilePath, "r+");
for(i = 1; i<=NumOfNames; i++)
{
while(fgets(LineOfText, sizeof(LineOfText), fpn))
{
TempName = strstr(LineOfText, Names[i]);
if(TempName != NULL)
{
MaxName++;
}
}
}
fclose(fpn);
}
I guess that one problem of the code is that it would have to read the file upon every iteration of i. Try to re-order the loops like this:
while(fgets(LineOfText, sizeof(LineOfText), fpn))
{
for(i = 1; i<=NumOfNames; i++)
{
TempName = strstr(LineOfText, Names[i]);
if(TempName != NULL)
{
MaxName++;
}
}
}
This reads a line, checks the occurrances of all names in that line and then goes on to the next line.
If you do it your way, you will be at the end of file for i == 1 already.

how do i create recursive directories for the following requirement in c?

i expect to have more than one million files with unique names. I have been told that if i put all this files in one or two directories the search speed for these files will be extremely slow. So i have come up with the following directory architecture.
I want the directory structure to branch out with 10 sub directories and the level of the sub directories will be 4. because the file names are guaranteed to be unique i want to use these file names to make hashes which can be used to put the file in a directory and also later to find it. The random hash values will make a directory to have,approximately, 1,000 files.
so if F is root directory then inserting or searching for a file will have to go through these steps:
I want to use numbers from 0-9 as directory names
h=hash(filename)
sprintf(filepath,"f//%d//%d//%d//%d//.txt",h%10,h%10,h%10,h%10);
HOW DO I CREATE THESE DIRECTORIES?
EDIT:
All the files are text files.
The program will be distributed to many people in order to collect information for a research. So tt is important that these files are created like this.
EDIT:
i created the following code to implement perreal's pseudo code. It compiles to success but gives the run time error given at the end.
error occurs at the sprintf() line.
#include<iostream>
#include<stdlib.h>
#include<windows.h>
void make_dir(int depth, char *dir) {
if (depth < 4) {
if (! CreateDirectoryA (dir,NULL))
for (int i = 0; i < 10; i++) {
sprintf(dir,"\\%d",i);
char *sdir=NULL ;
strcpy(sdir,dir);
CreateDirectoryA(sdir,NULL);
make_dir(depth + 1, sdir);
}
}
}
int main()
{
make_dir(0,"dir");
return 1;
}
Unhandled exception at 0x5b9c1cee (msvcr100d.dll) in mkdir.exe:
0xC0000005: Access violation writing location 0x00be5898.
Kind of pseudo code, but can be done like this:
void make_dir(int depth, char *dir) {
if (depth < 4) {
CreateDirectoryA (dir,NULL);
for (int i = 0; i < 10; i++) {
char *sdir= (char*)malloc(strlen(dir+10)); // XXX 10?
strcpy(sdir, dir);
sprintf(sdir + strlen(sdir), "\\%d", i);
printf("%s\n", sdir);
//CreateDirectoryA(sdir,NULL);
make_dir(depth + 1, sdir);
free(sdir);
}
}
}
}
And to call make_dir(0, rootdir);
Do not do this: sprintf(dir,"\%d",i);
dir is a const, read only string in your example.
You're likely to run off the end of the string, corrupting things that follow it in memory.
Do not copy to sdir without allocating memory first.
sdir = (char *)malloc( strlen( dir ) + 1 );
At the end of the function make_dir, you will have to call free( sdir ); so you do not leak memory.

a tester which does not know the names of files

There are some test cases for a program in format: ??.in ans ??.out in directories
./input and ./output such that for each test the first part of the names are equal e.g. test1.in, test1.out
How can I write a code which sweeps through these files together? (may be the files can be looked for in alphabetical order [in each directory]...)
Get list of all files in a directory using opendir (and related functions), and then parse the array.
for (int i = 0; i < 100; i++) {
char in_filename[100];
char out_filename[100];
sprintf(in_filename, "./input/test%d.in", i);
sprintf(out_filename, "./output/test%d.out", i);
/* use in_filename and out_filename as you see fit */
/* ... */
}

deleting a string from a particular position in a file

I want to delete a string from a particular position in the file. Is thre a function to do that?
Can I delete last line of a file through a function?
You have two option
To read whole file, remove what you need and write it back
If the file is big, read file sequentially, remove given part, and move content after that forward
No there is no such function that will let you do this directly on a file.
You should load up the file content in memory and modify there and write back to file.
I don't feel like looking up all the io functions, so here's pseudo-c on how to implement option 2 of ArsenMkrt's answer
char buffer[N]; // N >= 1
int str_start_pos = starting position of the string to remove
int str_end_pos = ending position of the string to remove
int file_size = the size of the file in bytes
int copy_to = str_start_pos
int copy_from = str_end_pos + 1
while(copy_from < file_size){
set_file_pos(file, copy_from)
int bytes_read = read(buffer, N, file)
copy_from += bytes_read
set_file_pos(file, copy_to)
write(buffer, file, bytes_read)
copy_to += bytes_read
}
truncate_file(file,file_size - (str_end_pos - str_start_pos + 1))
something to that effect

Resources