I am having two inputs :video1.y and video2.y .I want to Concatenate these two files to create one video.y file ??I am writing code in C .It may be basic question but not able to do that !! Both inputs are having same hight and width .
Code:
int main()
{
// Open two files to be merged
FILE *fp1 = fopen("D:\\dump\\video1.y", "rb");
FILE *fp2 = fopen("D:\\dump\\video2.y", "rb");
// Open file to store the result
FILE *fp3 = fopen("D:\\dump\\video.y", "wb");
char c; //Change char to int as per answer given by user3710044..
//...Which is working !!
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
// Copy contents of first file to video3.y
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
// Copy contents of second file to video3.y
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged video1.y and video2.y into video.y");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
video1.y and video2.y are of 55 MB .and my output is 3 KB.I am not able to concatenate this two inputs
The type of the c variable is a char this cannot hold the value for EOF and all 256 byte values.
As it happens on your machine char is a signed type so the cast return result from fgetc and the cast value of EOF actually mean that an EOF is found at the end of a file. The problem is that if the file contains an 0xFF byte this is also seen as an EOF.
In summary, change the type of c to an int.
Related
I need to write some code to read an equation from a text file and write the answer to a new one, and I'm completely stuck
I've managed to read the question and print it in terminal as characters, but that's it.
this new code can't even do that.
for those asking, the exact wording of the question is this:
"Read an input file: questions.txt; and produce a program that creates an output file answers.txt that includes
the question and the answer for each line in the questions file.
For example, if the line in questions is:
5*5
the answer file should read:
5*5 = 25
Allow for the following operations: +, -, *, /, % and for the correct order of operations. Also allow for at least 2
operators (3 operands), for example:
3+5*5 = 28"
My main problem is reading the equation from the text file and separating it into numbers and the operator symbol
FILE *fp;
int a, b, c, ch, i, number[100];
char input[5];
fp = fopen ("questions.txt", "r");
while(1)
{
ch = fgetc(fp);
if (ch == EOF)
{
break;
}
else
{
input[i] = ch;
}
}
fclose(fp);
printf("%s", input);
}
There are errors:
check file has been open correcly: if (fp != NULL)
i = 0; on begin, now we dont know value
input[i] = ch; => input[i++] = (char)ch;
if (ch == EOF) => if (ch == EOF && i < 4) 4 because 5 - 1 and move this to const or macro
After break of while input[i] = '\0';
You need to open the file you want to read from in read mode with the normal fopen('questions.txt', 'r') like you did. Next, you need to fopen('newFile.txt', 'w') you want to write to in w write mode. Use a while loop to get each character of the questions.txt file with getc(fileToRead) while you haven''t hit the end of the questions.txt file, hence ... != EOF, and write to the newFile.txt. The printf("%c", c) is to print each character as you read it from questions.txt.
FILE *fileToRead, *fileToWrite;
int c;
//open the questions.txt file in read mode
*fileToRead = fopen ("questions.txt", "r");
//create newFile.txt and open it in write mode
*fileToWrite = fopen ("newFile.txt", "w");
while ((c = getc(fileToRead)) != EOF) {
putc((char)c, fileToWrite );
printf("%c", (char)c);
}
fclose(fileToRead);
fclose(fileToWrite);
I'm making some small changes to a preexisting application. The app is writing lines of text to a buffer and then flushing the buffer. I'm not sure when it actually writes the text to the file, but I'm trying to copy everything in that buffer and write a copy of it all out to a completely different file.
Below is the last line of writing to the preexisting buffer before it eventually calls fflush().
fprintf(_log, "-- FINAL TEXT LINE --\n");
Below is my code that I'm using in an attempt to copy that buffer to a separate file which is dynamically named according to the log time. This custom-%ld.log does not already exist and needs to be created.
char tmp[sizeof(_log)];
sprintf(tmp, "custom-%ld.log", (long int)lf->time);
FILE *fp1, *fp2;
char a;
fp1 = _log;
fp2 = fopen(tmp, "a");
if (fp2 != NULL) {
do {
a = fgetc(fp1);
fputc(a, fp2);
} while (a != EOF);
fflush(fp1);
fclose(fp2);
}
fflush(_log);
I'm sure my mistakes are very basic, but I don't know what I'm doing. I've tried dozens of suggestions on other websites, and suggestions here from other questions, but I'm not having any luck.
Yes, there are a few mistakes in here.
This will allocate a buffer of 4 or 8 bytes depending on the word size of your computer. 'sizeof' is a compile time directive that gives you the size of the underlying type.
char tmp[sizeof(_log)];
So do this instead (where 100 is just a big enough number to hold the result):
char tmp[100];
Next using a char for 'a' will not be able to hold the EOF value. Use int.
int a;
By fixing the definition of 'a' your loop is now not infinite, but it will eventually write the constant EOF to the file, which will be some garbled character. Change it like so:
while ((a = fgetc(fp1)) != EOF) {
fputc(a, fp2);
}
So in the end you should have:
char tmp[100];
sprintf(tmp, "custom-%ld.log", (long int)lf->time);
FILE *fp1, *fp2;
int a;
fp1 = _log;
fp2 = fopen(tmp, "a");
if (fp2 != NULL) {
while ((a = fgetc(fp1)) != EOF) {
fputc(a, fp2);
}
fflush(fp1);
fclose(fp2);
}
fflush(_log);
You are writing EOF to file before checking it with
do {
a = fgetc(fp1);
fputc(a, fp2);
} while (a != EOF);
You could try
int a; // make sure you have the correct type
while((a = fgetc(fp1)) != EOF)
fputc(a, fp2);
}
EOF is not (except some legacy formats) part of the file. It is an indicator returned by file reading functions.
Note that fflush(fp1); is undefined behaviour when fp1 is opened for input.
You say "custom-%ld.log" does not already exist yet you open it with
fp2 = fopen(tmp, "a");
Which would append, if you forgot to delete a previous version. To make sure it is a new file, open with
fp2 = fopen(tmp, "w");
I'm new to C language and I'm trying to save data to a .csv and read the same data in a very simple program.
char c;
FILE *fp;
fp = fopen("file.csv", "w+");
fprintf(fp, "Hello;World\nLine");
fclose(fp);
fp = fopen("file.csv", "r");
while (getc(fp) != EOF) {
printf("%c", getc(fp));
}
fclose(fp);
I don't know why the output is wrong:
el;ol
ie
Thanks in advance
Because you are reading a character in the loop condition (so it prints out every other one when printing), and reading another one when printing it out. Try this:
int ch;
while ((ch=getc(fp)) != EOF) {
printf("%c", ch);
}
Here:
while (getc(fp) != EOF) {
printf("%c", getc(fp));
}
You are calling getc() twice every time through the loop, but only printing one character. So you get half te hrces rm te fl n ls h ohr hl.
I am currently learning C and trying to solve a problem. I have a text file name elements.txt which has the following:
elements.txt
Carbon (from Latin: carbo "coal") is a chemical element ..
Oxygen is a chemical element with symbol O ..
Platinum is a chemical element with symbol Pt ...
Silicon is a chemical element with symbol Si ...
Titanium is a chemical element with symbol Ti ...
I would like to create multiple files based on elements such as carbon.txt, oxygen.txt, platinum.txt, silicon.txt and titanium.txt from elements.txt.
This is my soure code:
void magic(){
FILE *fp,*fp1, *fp2, *fp3, *fp4, *fp5;
char *fn, *fn1, *fn2, *fn3, *fn4, *fn5;
int ch;
fn1 = "new/carbon.txt";
fn2 = "new/oxygen.txt";
fn3 = "new/platinum.txt";
fn4 = "new/silicon.txt";
fn5 = "new/titanium.txt";
fn = "elements.txt";
// Read file
fp = fopen(fn ,"r");
if(fp == NULL){
printf("Error opening %s for reading. Program terminated",fn);
}
// Write file
fp1 = fopen(fn1, "w");
fp2 = fopen(fn2, "w");
fp3 = fopen(fn3, "w");
fp4 = fopen(fn4, "w");
fp5 = fopen(fn5, "w");
if(fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL || fp5 == NULL){
printf("Error opening %s for wrting. Program terminated",fn);
}
while( (ch= fgetc(fp)) != '\n')
fputc(ch, fp1);
while( (ch= fgetc(fp)) != '\n')
fputc(ch, fp2);
while( (ch= fgetc(fp)) != '\n')
fputc(ch, fp3);
while( (ch= fgetc(fp)) != '\n')
fputc(ch, fp4);
while( (ch= fgetc(fp)) != EOF)
fputc(ch, fp5);
printf("All files were created successfuly!.\n");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
fclose(fp5);
fclose(fp);
}
int main(){
magic();
return 0;
}
I do get my files created but oxygen gets printed in platinum and platinum,silicon and titanium gets printed in titanium. It seems like there is a mistake,when I am doing a while loop reading the character. Not sure how to fix this issue.And is there a way to read multiple and write multiple files using for-loop?
Any help would be greatly appreciated!thanks
As comments suggest, in order to make this block correct, you should add a return statement:
if(fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL || fp5 == NULL){
printf("Error opening %s for wrting. Program leaving",fn);
return; //add this statement to leave function
}
Otherwise, the statement Program terminated will be a lie.
If you want your code to handle input files that have lines with only spaces (instead of modifying your input files), add a test for lines with only spaces before putting that line in your output file. Using fgets(), strstr() and strlen() would be suited to creating such a test.
In your .txt input file, contents for different elements are separated by two newline characters, not one. Try rewrite it:
Carbon is...
Oxygen is...
etc. (without extra newline at the end of a paragraph).
Alternatively, you can check for the presence of 2 newlines instead of 1, but this will be more complicated.
(Explanations: The program writes about carbon to fn1, then sees a newline and switches to fn2, then sees another newline and switches to fn3. The same thing happens at the end of the oxygen paragraph).
I am trying to read the number of lines of a file in Ubuntu. For my code I'm using CodeBlocks.
This is the code I've made.
int countlines()
{
// count the number of lines in the file called filename
FILE *fp = fopen("words", "r");
int ch=0;
int lines=0;
if (fp == NULL){
return 0;
}
lines++;
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
fclose(fp);
return lines;
}
If I call countlines(), the return value is a 0, that is because he checks if fp==NULL, and that is true.
I placed words in the same folder as my main. The executable file is in Projectfolder/bin/Debug.
Words looks like this:
"albatros",
"olifant",
"kantklos",
"robijn",
"internet"
The final goal is to fill an array with the words of the file words, without using #include "words".
Check what the working directory is set to. It might not be pjt/bin/Debug. Also, try specifying full path to the file.
if (fp == NULL){
return 0;
}
fp is checked with NULL, because, fopen returns pointer, if it succeed, it will be non-NULL, so if fp == NULL, then file open does not succeed. That's why program cannot proceed, and just return.
Hmmm!!! It could have been the fact that the file extension for "words" was not specified. Otherwise, I couldn't find anything else wrong with the program.