I have a text file containing 5 words, and the second text file containing 2000 words I want to write a program in C language calculates the number of repeat words from first file in the second file and print the result on screen and I am new in C language , can any one help me to do it ...Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_line(FILE *in, char *buffer, size_t max)
{
return fgets(buffer, max, in) == buffer;
}
int main(void)
{
char b[2000];
int wcount = 0;
int j;
char i[5];
char File_path[40];
char stuff[5] = "False";
FILE *file1;
FILE *file2;
file1=fopen("test.txt", "r"); // my word list text file
file2=fopen("dickens-chimes-379.txt","r"); // my text file
for (j = 0; j < 5 && strcmp(i, stuff); j++)
{
fscanf(file1,"%s",i);
while (fgets(b,2000, file2)!=NULL)
{
if((strcat(b,i)) ==NULL)
{
wcount=0;
}
wcount++;
}
printf("%s %d\n",i,wcount);
wcount=0;
}
fclose(file1);
fclose(file2);
}
my input is (test.txt) have words (love,like,book,go, and test)
the output is love 4296 like 0 book 0 go 0 test 0
i need the real value of the words occurrence in file2
i found this code in the net and i don't know how to modify it because
i an new to c language .. i hope you could help to to write code for
same thing
I feel to mention that this is a fairly complicated problem for a beginner. I would suggest to get warmed up with simpler problems first.
Nevertheless, here are some high level thoughts:
Separate I/O from business logic
Read test.txt in a array of 5 words
Read a dickens.txt in a character array.
[If the file is really really big, then this strategy may need to be modified.]
Pass the test array and dickens array to the core function, say repeatFinder()
Very very high level pseudocode:
for each word w in test array:
scan dickens array
if w occurs in dickens:
w_counter +=1
advance dickens array
In C, you can use strstr( dickens, w ) [link] to find if w is present in dickens
Related
this may seem basic to some of you, but please try to help me!
I have created a BMP file that stores a 4 x 3 image. The content of this file (FILEHEADER, FILEINFOHEADER, color bytes, and padding, are represented in hex).
I'm trying to write a loadFile function to load the BMP file and store it into a 2D array of integers.
I'm first trying to figure out how to store the content of the file into a character array, then i would try and do a strcpy of the char array and convert that into int values (i'm not converting yet).
But for now i've attempted to load the file and put into a char array... (it doesn't work, but i was hoping to get some feedback on whether or not i'm in the right direction), and how would i go about approaching this problem!
#include <stdio.h>
#include <stdlib.h>
#define length 30
#define rows 7
int main()
{
FILE *myBmp;
myBmp = fopen("myBmp.bmp", "r"); //for now i'm not sure what the r is for
//read file into array
char hexArray[rows][length];
int c;
for (c = 0; c < rows; c++) {
fscanf(myBmp, "%c", &hexArray[c]);
}
for (c = 0; c < rows; c++) {
printf("Number is: %c\n", hexArray[c]);
}
fclose(myBmp);
}
I'm trying to replace words that are passed in with the word "CENSORED" but I can't figure out where to account for the difference between the replaced word and censored. Here's an example of the input and output.
./a.out Ophelia draw or <poem.txt
Said Hamlet to CENSORED,
I'll CENSOREDsketch of thee,
What kind of pencil shall I use?
2B CENSORED2B?
But the correct output should be:
Said Hamlet to CENSORED,
I'll CENSORED a sketch of thee,
What kind of pencil shall I use?
2B CENSORED not 2B?
Full code:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
char fileRead[4096];
char replace[] = "CENSORED";
int arg=0;
size_t word_len = strlen(argv[arg]);
while (fgets(fileRead, sizeof(fileRead), stdin) != 0)
{
char *start = fileRead;
char *word_at;
for (arg = 1; arg < argc; arg += 1) {
if ((word_at = strstr(start, argv[arg])) != 0) {
printf("%.*s%s", (int)(word_at - start), start, replace);
start = word_at + word_len -1;
}
}
printf("%s", start);
}
printf("\n");
return (0);
}
I really appreciate any tips! Thanks :)
Create a temporary output char array of size 4096 (assuming line length doesn't exceeds 4096) for storing a complete processed line. It would be better if you read word by word from the file.
Compare the read word with each of the replaceable words (Ophelia, draw, or), if it doesn't need to be replaced append the word to the output char array with a whitespace. If the word has to be replaced then append the word CENSORED to the output char array.
If you reach a new line printf the entire output char array, reuse the output array and repeat the process until you reach EOF
Change the way you are processing the text. For each block you read, for each input word to censor, perform the strstr. Find the minimum - that is, the earliest censored word. Divide the block into before-the-word, word, and after-the-word parts. Emit the before-the-word part. Skip the word part. Repeat with the after-the-word part until it's time for a new block.
How do i read some 5 to 10 characters from a sample txt file using an fread funtion.
I have the following code:
#include <stdio.h>
main()
{
char ch,fname[20];
FILE *fp;
printf("enter the name of the file:\t");
gets(fname);
fp=fopen(fname,"r");
while(fread(&ch,1,1,fp)!=0)
fwrite(&ch,1,1,stdout);
fclose(fp);
}
when i enter any sample filename..it prints all the data of the file.
my question is how to print only the first 5 to 10 characters from the sample file.
Your while loop runs until read reaches the end of the file (reads 0 bytes for the first time).
You will want to change the condition by using a for loop or a counter.
i.e. (these are suggestions, not the full working code):
int counter = 10;
while(fread(&ch,1,1,fp)!=0 && --counter)
fwrite(&ch,1,1,stdout);
or
int i;
for(i=0; i < 10 && fread(&ch,1,1,fp) > 0 ; i++)
fwrite(&ch,1,1,stdout);
Good luck!
P.S.
To answer your question in the comments, fread allows us to read the data in "atomic units", so that if a whole unit isn't available, no data will be read.
A single byte is the smallest unit (1), and you are reading one unite (of a single byte), this is the 1,1 part in the fread(&ch,1,1,fp).
You could read 10 units using fread(&ch,1,10,fp) or read all the bytes unrequited for a single binary int (this won't be portable - it's just a demo) using int i; fread(&i,sizeof(int),1,fp);
read more here.
Here is a modified version of your code. Check the comments at the lines that are modified
#include <stdio.h>
#define N_CHARS 10 // define the desired buffer size once for code maintenability
int main() // main function should return int
{
char ch[N_CHARS + 1], fname[20]; // create a buffer with enough size for N_CHARS chars and the null terminating char
FILE *fp;
printf("enter the name of the file:\t");
scanf("%20s", fname); // get a string with max 20 chars from stdin
fp=fopen(fname,"r");
if (fread(ch,1,N_CHARS,fp)==N_CHARS) { // check that the desired number of chars was read
ch[N_CHARS] = '\0'; // null terminate before printing
puts(ch); // print a string to stdout and a line feed after
}
fclose(fp);
}
Can someone please help explain the difference in C and how my file fails to merge, and appends instead? The background is I am on an online computer science class this summer which uses C language, all we are given is read the entire texbook (732 pages), and do 20 projects over the course of 8 weeks, with no actual instruction, lecture, slides, or explanations. Please explain it so I can learn as I need to actually understand these terms to be better and progress in my Electrical Engineering program. I have contacted my professor for help but the answers are always short and uninformative, and you guys have provided so much more quality feedback to date. Also I do get that he is right and it was appended and not merged, but any feedback how i could of rectified this will help as well. Thank you again!
#include <stdio.h>
#include <stdlib.h>
FILE *inptr1, *inptr2, *outptr;
int main()
{
char c;
char file1[30], file2[30], file3[30];
printf("Enter the first files name\n");
gets(file1);
printf("Enter the second files name\n");
gets(file2);
printf("Enter the file name that will store the data from the other two files\n");
gets(file3);
inptr1 = fopen("C:\\Users\\Eric\\Desktop\\file1.txt","r");
inptr2 = fopen("C:\\Users\\Eric\\Desktop\\file2.txt","r");
if( inptr1 == NULL || inptr2 == NULL )
{
perror("Error ");
printf("Press any key to exit!\n");
exit(1);
}
outptr = fopen("C:\\Users\\Eric\\Desktop\\file3.txt","w");
if( outptr == NULL )
{
perror("Error ");
printf("Press any key to exit!\n");
exit(1);
}
while( ( c = fgetc(inptr1) ) != EOF )
fputc(c,outptr);
while( ( c = fgetc(inptr2) ) != EOF )
fputc(c,outptr);
printf("The two files were sucessfully merged into %s \n",file3);
fclose(inptr1);
fclose(inptr2);
fclose(outptr);
return 0;
}
Page 611 #5 Write a complete C program. Use the two files provided. Your program should not assume that you know which file is shorter.
5) Write a function to merge two sorted files of names and write the names to a
new file.
Was the only direction I was given as far as to what the program was supposed to do.
and this was the feedback i received, "Did you look at the resulting file?The girls names are all at the bottom. You were to merge the files,not append the files."
Thank you again in advance for your valuable feedback as it has taught me so much to date.
You're going to need to look at the contents of each file and write them to the third file in sorted order. That seems to be what the assignment is asking you to do. What you're doing currently is dumping the contents of file1.txt into file3.txt followed by the contents of file2.txt. You want to end up with a sorted list containing the contents of both files.
There are many ways to accomplish your requirements. This is just one that jumps out:
Steps to perform:
1 Open two existing files (fopen(fp, "r");), and one new file (fopen(fp, "w");)
2 write all lines of first file, then the second file to the third file. (fopen(), fgets(), fputs(), fclose(), etc.)
3 Read third file into an array of strings, keeping a count of the total number of strings read. close file.
4 Sort array of strings. (qsort())
5 Open third file, Write sorted array of strings into that file.
6 Close all files, free all memory.
Note, using this method, it does not matter if the two original files are sorted, or not, (Assignment says they are, but does not matter). The qsort routine will completely sort the string array either way.
qsort()
Most of the functions referenced are straight forward to use. qsort() is a little weird.
Here is an example showing how to set up qsort() for use sorting an array of strings:
For an array of strings: strings with the number of strings being say: cnt then:
qsort(strings, cnt, sizeof(char*), sortstring);
//With the function sortstring defined as:
static int sortstring( const void *str1, const void *str2 )
{
const char *rec1 = *(const char**)str1;
const char *rec2 = *(const char**)str2;
int val = strcmp(rec1, rec2);
return val;
}
string arrays
Creating an array of strings can also be challenging. Again, there are several ways to do this, here are two:
If you know the dimensions of each line, and the total number of lines in both files, then you can do it like this:
char strArray[numLines][longestLine];
If you don't, then you have to determine that at run-time by getting a count of the total number of lines, say when you are reading them from each file. And you will also need the length of the longest line found in both files, say by using strlen() on each one at some point as it is read or written. Once you have that information, you can create your string array like this:
char **strings=0;
Then, before you need it, create memory for it:
char **allocMemoryStr(char **strings, int numStrings, int maxLen)
{
int i;
strings = calloc(sizeof(char*)*(numStrings+1), sizeof(char*));
for(i=0;i<numStrings; i++)
{
strings[i] = calloc(sizeof(char)*maxLen + 1, sizeof(char));
}
return strings;
}
Finally, when you are finished using dynamically allocated memory, you must always free it:
void freeMemoryStr(char **strings, int numStrings)
{
int i;
for(i=0;i<numStrings; i++)
if(strings[i]) free(strings[i]);
free(strings);
}
I've got a very large text file that I'm trying to do word analysis on. Among word count, I might be looking for other information as well, but I left that out for simplicity.
In this text file I have blocks of text separated by asterisks '*'. The code I have below scans the text file and prints out # of characters and words as it should, but I'd like to reset the counter after an asterisk is met, and store all information in a table of some sort. I'm not so worried on how I'll make the table as much as I am unsure of how to loop the same counting code for each text block between asterisks.
Maybe a for loop like
for (arr = strstr(arr, "*"); arr; arr = strstr(arr + strlen("*"), "*"))
Example text file:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I have a sentence. I have two sentences now.
*
I have another sentence. And another.
*
I'd like to count the amount of words and characters from the asterisk above this
one until the next asterkisk, not including the count from the last one.
*
...
...
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
(EOF)
Desired output:
*# #words #alphaChar
----------------------------
1 9 34
-----------------------------
2 5 30
-----------------------------
3 28 124
...
...
I have tried
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int characterCount=0;
int counterPosition, wordCount=0, alphaCount=0;
//input file
FILE *file= fopen("test.txt", "r");
if (file== NULL)
printf("Cannot find the file.\n");
//Count total number of characters in file
while (1)
{
counterPosition = fgetc(speechFile);
if (counterPosition == EOF)
break;
++characterCount;
}
rewind(file); // Sends the pointer to the beginning of the file
//Dynamically allocate since array size cant be variable
char *arr= ( char*) malloc(totalCharacterCount);
while(fscanf(speechFile, "%c", &arr[i]) != EOF ) //Scan until the end of file.
i++; //increment, storing each character in a unique position
for(i = 0; i <characterCount; i++)
{
if(arr[i] == ' ') //count words
wordCount++;
if(isalpha(arr[i])) //count letters only
alphaCount++;
}//end for loop
printf("word count is %d and alpha count is %d", wordCount,alphaCount);
}
Since you are having full files text in array arr[], you need to divide that string arr using * as delimiter. you can use strtok() to divide that string using * as delimiter. Then perform the word count and character count operation on each token. read this link to know about strtok.