Creating a new file with a different extention [closed] - c

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);

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).

How does this program work C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
void fade(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
float v = (float)f/255;
for (int i = 0; i < imagen1->tamanyo; i++) {
imagen1->datos[i] = (imagen1->datos[i] - imagen2->datos[i])*v + imagen2->datos[i];
}}; //end of fade
It's supose to fade two images into a single one.
If you rewrite the equation, you will end up with:
imagen1->datos[i] = v*imagen1->datos[i] + (1-v)*imagen2->datos[i];
This is how the blend works. You are specifying how much of the first image (out of 255 parts) should be included in the function parameter f, 255-f parts will be included from the second image.

how to create multiple files in ubuntu [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
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");

How to get the following output in C programming [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
A logical question asked in the aptitude test.
if(______)
{
printf("Hi");
}
else
{
printf("Hello");
}
What condition should be provided in place of _____ to get the following output ?
HelloHello
You do not need to give control to else twice. All you need is a false condition with a side effect of printing the other "Hello", for example
if (printf("Hello") == 0) // this condition is false, because printf returns the number of chars written
{
printf("Hi");
}
else
{
printf("Hello");
}

Resources