how to create multiple files in ubuntu [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why is this code not writing to different files?
---file name: "file.c". c
int main(){
// skipped rest of the code
FILE * pfile;
while(i<25)
{
sprintf(mytext,"%d.txt", i); // trying to make mytext1.txt, mytext2.txt ...
pfile = fopen ("mytextd.txt","w"); // trying to write in each files "confuse here"
printf("eneter in server recieve");
if(pfile != NULL)
{
// while(i<25)
// {
read(connfd,sendBuff,sizeof(sendBuff));
fputs(sendBuff,pfile);
fputs(sendBuff,stdout);
i++;
// }
fclose (pfile);
}
}
}

You loop keeps writing to the same file "mytestd.txt" because you are using that string constant instead of the variable mytext that you just wrote the desired filename into.
You probably mean
pfile = fopen (mytext,"w");

Related

Is "segmentation fault" caused from fopen line a stack overflow problem in C? (darknet, detector.c) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I am experiencing a darknet YOLOv4. I want to add detection result logging line to this mAP example code.
I think I wrote a line that writes files according to the grammar, but the "segmentation fault" error occurs in the line where "fopen" is operated.
The line like printf works well.
The validate_detector_map() part of the detector.c of darknet is attached below.
https://github.com/AlexeyAB/darknet/blob/master/src/detector.c#L940
And below is the part I added.(////ju added~ //////////)
... // For multi-class precision and recall computation
float *avg_iou_per_class = (float*)xcalloc(classes, sizeof(float));
int *tp_for_thresh_per_class = (int*)xcalloc(classes, sizeof(int));
int *fp_for_thresh_per_class = (int*)xcalloc(classes, sizeof(int));
for (t = 0; t < nthreads; ++t) {
args.path = paths[i + t];
args.im = &buf[t];
args.resized = &buf_resized[t];
thr[t] = load_data_in_thread(args);
}
////ju added //파일 열려야 하는 위치
printf("\nlogging start.. \n\n");
FILE *log_csv;
printf('%d\n', fileno(log_csv));
if(!(fopen("/home/aicar/darknet/0_log_ju0.csv", 'a')))
{
printf("failed to open csv file\n\n");
exit(1);
}
else
{
printf("csv file opened\n\n");
fprintf(log_csv, "timestamp, class_id, gt, conf_mat, \n" ); //header row
fclose(log_csv);
}
//////////
time_t start = time(0);
for (i = nthreads; i < m + nthreads; i += nthreads) { ......
Is this a stack overflow problem? The reason I thought that was because the author used the free function.
fopen returns a file handle. Just a couple lines above you're defining a variable FILE *log_csv; which would be probably where you want to put that handle.
Furthermore the second argument of fopen is a C-string (i.e. a pointer to a NUL terminated array of chars). TTBT, I'm surprised your compiler did let this pass.
Try this:
FILE *log_csv = fopen("/home/aicar/darknet/0_log_ju0.csv", "a");
if( !log_csv ){
printf("failed to open csv file\n\n");
exit(1);
} else {
printf("%d\n", fileno(log_csv));
…
}
This code is also dangerous:
FILE *log_csv;
printf('%d\n', fileno(log_csv));
The fileno() function will attempt to dereference log_csv, which does not point to a FILE structure and therefore extremely likely to cause a SIGSEGV crash.

Using Regular Expression to extract word in a string in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to extract a small substring from a string of words in c. I did it in a previous program written in python using this code:
out_block = "".join(re.findall(r'.FT off(.*?).FT on',finaltext,re.DOTALL))
The code extracts all the characters in between .FT off and .FT onand passes it to out_block as a string.
I wanted to know how to do the same using regular expressions but in C
How would I convert this code to a C code that does the exact same thing?
Basicly you do this:
#include <sys/types.h>
#include <regex.h>
regex_t finder;
if (0 != regcomp(&finder, "regex-string", 0)) { error handling }
You might need to adjust your regex string above.
After that you can do any number of times:
regmatch_t match[N_MATCH];
if (0 == regexec(&finder, "haystack", N_MATCH, match, 0)) {
process matches
} else {
no match
}
Clean up using regfree and get error messages (in regcomp) using regerror.
For more details, consult man page regex(3).

thread-safe variables comaprison C VS2010 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
threaded programming
I want to write simple multi-thread app.
where each thread when it open I increment (using InterlockedIncrement) member by one ,
and decrements it (using InterlockedDecrement) when thread finishes
I know about Mutex/Semaphore/event
but I would more clean /simple way to implement comparison similar to the Interlocked function .
What I need next is implement comparison function [if(member == x)]
simple example:
Thread 1 function:
{
//do somthing
InterlockedDecrement(member);
}
Thread 2 function:
{
//do something else
InterlockedDecrement(member);
}
main thread function :
{
while(member)//<--how can it be done in thread safe fashion
{
//do yet another something
}
}
Use InterlockedAdd and add 0. This will lock the member and return the value without changing it:
while (InterlockedAdd(&member, 0) == someValue)
{
//do yet another something
}

How to modify the function to satisfy OCP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The following function is implemented in C:
function(struct_XX *p)
{
if(p->A)
{
if(p->B)
{
do something0;
}
if(p->C)
{
do something1;
}
if(p->D && p->E)
{
do something2;
}
if(p->Z)
{
do something3;
}
}
}
each branch has different things to do,It doesn't satisfy Open-Closed Principle(because the struct which p points at is not stable ,new fields will be added into it often,that means new process codes will be added into function frequently); how can it be modified to satisfy OCP?
you haven't given much to go, you have a bunch of conditions, and a bunch of "do"s based on those conditions.
So given that... you can make something that stores a collections of conditions and callbacks....
void ocpfunction(struct struct_XX *p)
{
int i;
for(i=0; i<p->conditions_count; i++)
{
if(p->conditions[i].evaluate(p))
{
p->conditions[i].callback(p, p->conditions[i].context);
}
}
}

Creating a new file with a different extention [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my project I have one file as input, and I need the output file with a new extension. Suppose my input file is file.txt here as name. I need output file as file.dic. I tried the following.
fpout = fopen(strcat(name,".dic"), WRITE_ONLY);
I know it's not the correct way. But what should I do to get the file name as file.dic?
char * newstr;
newstr = strstr (name,"txt");// name = "file.txt"
strncpy (newstr,"dic",3);
//newstr = file.dic
this will replace the file.txt with file.dic
If you just want to change the file extension, this is an easy way to do it.
int lenght;
lenght = strlen(name);
name[lenght - 1] = c;
name[lenght - 2] = i;
name[lenght - 3] = d;
name[lenght - 4] = .;
you can also use while, and while you don't check a '.' increment i, then change the extension in the same way.
or
strncpy (strstr (name,"txt"),"dic",3);

Resources