I'm trying to write a function that given an array of text files, it prints all lines that share the same index on the same line, for example:
file1:
hey
there
file2:
1
2
output:
hey 1
there 2
I can't seem to find a way of iterating through the lines of all the files like someone would do on an array. This is where i'm stuck at:
void paste(FILE** files,int argc){
int maxLines = getMaxLines(files,argc);
int line = 1;
//char buf[256];
char ch;
for (int i=0; i<maxLines; i++){
for (int j=1; j<argc; j++){
while ((ch=fgetc(files[j])) != '\n'){
putchar(ch);
}
}
printf("\n");
line++;
}
//printf("%d",maxLines);
}
Obviously it doesnt do what is intended but i think i have the main idea captured in that piece of code. How can i use the line index to iterate through lines? How can i access each line this way?
Create a function that prints a line from a file - basically, your while loop (no need for line index, since the position in the file is persistent, meaning that each time you call the function for some file, the next line will be printed).
Now iterate over the files, printing one line from each, again and again, until one of them reaches eof.
Related
I wanted to find the number of occurences of some specific words
which are in the file2 as;
(word1)
(word2)
(word3)
For the first word, everything works fine as it finds the no. of occurences. But for the other two words the occurences equal to 0. Even though I tried to debug the program, I could not figure out why the other two words are skipped by the program. Because I am a beginner, maybe I did a mistake in the conditions of the two while loops, while I try to access each word.
Could anyone help me to understand where am I doing wrong?
I appreciate your help
void count_words(FILE *file1, FILE *file2){
char words[20], check_words[20];
int occurrences = 0;
while (fscanf(file2, "%s", words) != EOF){
while (fscanf(file1, "%s", check_words) != EOF){
for (int i=0; i<strlen(check_words); i++){
check_words[i] = tolower(check_words[i]);
}
if (strcmp(check_words, words) == 0){
occurrences++;
}
}
printf("'%s' -> %d occurrence(s)\n", words, occurrences);
occurrences = 0;
}
}
When you read from a file, the file pointer (like a cursor) moves.
The first time you read a word from file1, your loop will read all the words from file2. Now the file pointer is at the end of file2.
So the second time you read a word from file1, when you try to read a word from file2 you will fail, because file2 file pointer is at the end of the file. There are no more words - you used them all first time.
You need to either rewind file2 using the fseek(file2, 0, SEEK_SET) function to put the file pointer back to the start, or you need to solve the problem in a different way.
For example it is common to read the whole of both files into a data structure in memory and then match them from that data structure.
So I need to create a word search program that will read a data file containing letters and then the words that need to be found at the end
for example:
f a q e g g e e e f
o e q e r t e w j o
t e e w q e r t y u
government
free
and the list of letters and words are longer but anyway I need to save the letters into an array and i'm having a difficult time because it never stores the correct data. here's what I have so far
#include <stdio.h>
int main()
{
int value;
char letters[500];
while(!feof(stdin))
{
value = fgets(stdin);
for(int i =0; i < value; i++)
{
scanf("%1s", &letters[i]);
}
for(int i=0; i<1; i++)
{
printf("%1c", letters[i]);
}
}
}
I also don't know how I am gonna store the words into a separate array after I get the chars into an array.
You said you want to read from a data file. If so, you should open the file.
FILE *fin=fopen("filename.txt", "r");
if(fin==NULL)
{
perror("filename.txt not opened.");
}
In your input file, the first few lines have single alphabets each separated by a space.
If you want to store each of these letters into the letters character array, you could load each line with the following loop.
char c;
int i=0;
while(fscanf(fin, "%c", &c)==1 && c!='\n')
{
if(c!=' ')
{
letters[i++]=c;
}
}
This will only store the letters and is not a string as there is no \0 character.
Reading the words which are at the bottom may be done with fgets().
Your usage of the fgets() function is wrong.
Its prototype is
char *fgets(char *str, int n, FILE *stream);
See here.
Note that fgets() will store the trailing newline(\n) into string as well. You might want to remove it like
str[strlen(str)-1]='\0';
Use fgets() to read the words at the bottom into a character array and replace the \n with a \0.
and do
fgets(letters, sizeof(letters, fin);
You use stdin instead of the fin here when you want to accept input from the keyboard and store into letters.
Note that fgets() will store the trailing newline(\n) into letters as well. You might want to remove it like
letters[strlen(letters)-1]='\0';
Just saying, letters[i] will be a character and not a string.
scanf("%1s", &letters[i]);
should be
scanf("%c", &letters[i]);
One way to store the lines with characters or words is to store them in an array of pointers to arrays - lines,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLET 500
#define MAXLINES 1000
int main()
{
char *lptr;
// Array with letters from a given line
char letters[MAXLET];
// Array with pointers to lines with letters
char *lineptr[MAXLINES];
// Length of current array
unsigned len = 0;
// Total number of lines
int nlines = 0;
// Read lines from stdin and store them in
// an array of pointers
while((fgets(letters,MAXLET,stdin))!=NULL)
{
len = strlen(letters);
letters[len-1] = '\0';
lptr = (char*) malloc(len);
strcpy(lptr, letters);
lineptr[nlines++]=lptr;
}
// Print lines
for (int i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
// Free allocated memory
for (int i = 0; i < nlines; i++)
free(lineptr[i]);
}
In the following, pointer to every line from stdin is stored in lineptr. Once stored, you can access and manipulate each of the lines - in this simple case I only print them one by one but the examples of simple manipulation are shown later on. At the end, program frees the previously allocated memory. It is a good practice to free the allocated memory once it is no longer in use.
The process of storing a line consists of getting each line from the stdin, collecting it's length with strlen, stripping it's newline character by replacing it with \0 (optional), allocating memory for it with malloc, and finally storing the pointer to that memory location in lineptr. During this process the program also counts the number of input lines.
You can implement this sequence for both of your inputs - chars and words. It will result in a clean, ready to use input. You can also consider moving the line collection into a function, that may require making lineptr type arrays global. Let me know if you have any questions.
Thing to remember is that MAXLET and especially MAXLINES may have to be increased for a given dataset (MAXLINES 1000 literally assumes you won't have more than a 1000 lines).
Also, while on Unix and Mac this program allows you to read from a file as it is by using $ prog_name < in_file it can be readily modified to read directly from files.
Here are some usage examples - lineptr stores pointers to each line (array) hence the program first retrieves the pointer to a line and then it proceeds as with any array:
// Print 3rd character of each line
// then substitute 2nd with 'a'
char *p;
for (int i = 0; i < nlines; i++){
p = lineptr[i];
printf("%c\n", p[2]);
p[1] = 'a';
}
// Print lines
for (int i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
// Swap first and second element
// of each line
char tmp;
for (int i = 0; i < nlines; i++){
p = lineptr[i];
tmp = p[0];
p[0] = p[1];
p[1] = tmp;
}
// Print lines
for (int i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
Note that these examples are just a demonstration and assume that each line has at least 3 characters. Also, in your original input the characters are separated by a space - that is not necessary, in fact it's easier without it.
The code in your post does not appear to match your stated goals, and indicates you have not yet grasp the proper application of the functions you are using.
You have expressed an idea describing what you want to do, but the steps you have taken (at least those shown) will not get you there. Not even close.
It is always good to have a map in hand to plan to plan your steps. An algorithm is a kind of software map. Before you can plan your steps though, you need to know where you are going.
Your stated goals:
1) Open and read a file into lines.
2) Store the lines, somehow. (using fgets(,,)?)
3) Use some lines as content to search though.
4) Use other lines as objects to search for
Some questions to answer:
a) How is the search content distinguished from the strings to search
for?
b) How is the search content to be stored?
c) How are the search words to be stored?
d) How will the comparison between content and search word be done?
e) How many lines in the file? (example)
f) Length of longest line? (discussion and example) (e & f used to create storage)
g) How is fgets() used. (maybe a google search: How to use fgets)
h) Are there things to be aware of when using feof()? (discussion and examaple feof)
i) Why is my input not right after the second call to scanf? (answer)
Finish identifying and crystallizing the list of items in your goals, then answer these (and maybe other) questions. At that point you will be ready to start identifying the steps to get there.
value = fgets(stdin); is a terrible expression! You don't respect at all the syntax of the fgets function. My man page says
char *
fgets(char * restrict str, int size, FILE * restrict stream);
So here, as you do not pass the stream at the right place, you probably get an underlying io error and fgets returns NULL, which is converted to the int 0 value. And then the next loop is just a no-op.
The correct way to read a line with fgets is:
if (NULL == fgets(letters, sizeof(letters), stdin) {
// indication of end of file or error
...
}
// Ok letters contains the line...
I'm just trying to get fscanf to read all the characters in a file, along with all the words, but whenever I try to run a whileloop on the file I opened twice it doesn't seem to work? It only seems to be able to use fscanfon a file one time. I found a work around where I can scan the same file twice except I need to open the file a 2nd time for this to work. How can I use fscanf on the same instance of a file twice?
/*Description: Program will open a file named Story.txt
then counts the number of words and characters
and prints them out */
int main(){
char word[225]; // will be used to hold words (array of chars)
char c; // will be used to hold chars
int wordCount = 0; // will be used to hold the number of words in the file
int charCount = 0; // will be used to hold the number of chars in the file
FILE* wordFile = fopen("Story.txt","r"); // opens the file Story.txt for counting words
printf("Words: "); // indicates that the following outout will be the words of the file
while(fscanf(wordFile,"%s",&word)==1){ // a loop to scan the file for all the words in it until the end of the file
printf(" %s ",word); // prints out the word in the given cycle
wordCount = wordCount + 1; // keeps count of the words the loop has scanned up till now
}
fclose(wordFile); // closes wordFile and frees the memory
FILE* charFile = fopen("Story.txt","r"); // opens file Story.txt for counting chars
printf("\n\nChars: "); // indicates the following output will be the chars of the file
while(fscanf(charFile,"%c",&c)==1){ // a loop to scan the file for all the chars in it until the end fot he file
if(c!=' '){ // will check if the char is a space, if it is it will not count it
printf(" %c ",c); // prints out the char for the given cycle
charCount = charCount + 1; // keeps count of the chars the loop has scanned up till now
}
}
fclose(charFile); // closes charFile and frees the memory
printf("\n\nWord count: %d and Char count: %d",wordCount,charCount-1); // prints out the word count and char count
return 0;
}
As you can see i have to create two instances of the file or else it will not work. The first instance is called wordFile and the 2nd instance is called charFile. Here's the thing though: Both loops work, it's just that I can't use them on the same file twice. How can I make it so that I will only need to open one instance of the file and then use it to count both the words and the chars in it?
Things I tried: adding the space as suggested here didn't work: C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf (i searched fscanf but scanf is all that came up so i went off of that).
Another work around that I found strange: if i use wordFilein the 2nd while loop to search for chars it works, the only problem is I have to declare fclose(wordFile); right before it's used in the while loop. I thought fclosewas suppose to close the file and make it unusable? Anyways that worked but what I really want is to use use one instance of the file to read all the chars and strings in it.
Do something like below - the lazy way of course
char word[225];
char c;
int wordCount = 0;
int charCount = 0;
FILE* wordFile = fopen("Story.txt","r");
printf("Words: ");
while(fscanf(wordFile,"%s",word)==1){ // word gives the address not &word
wordCount = wordCount + 1;
}
printf("%d\n",wordCount);
fseek(wordFile,0,SEEK_SET); // setting the file pointer to point to the beginning of file
printf("Chars: ");
while(fscanf(wordFile,"%c",&c)==1){
if(c!=' '&& c!='\n' && c!='\t'){
charCount = charCount + 1;
}
}
printf("%d\n",charCount);
fclose(wordFile); // Closing the file once for all
return 0;
fscanf(wordFile,"%s",&word);
The variable "word" is the pointer to the first element of your array (string), so here you are scanning the address not the value. You should rather use:
fscanf(wordFile,"%s",word);
Let's say I'm reading in numbers from a text file. The text file consists of one-hundred fifty digit numbers (those are separate, there are 100 instances of 50 digit numbers).
I wanted to save each number as a row of a 2D array. To do this, I declared an array
char input[99][50] //50 columns to utilize the newlines in the text file
But it wouldn't read in the entire text file, even though, it seemed to me, it was the right size. It read in through the 99th number. For the 100th line, it printed a newline then a bunch of garbage symbols, etc. Please see the following:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
//Set up the use of the input text file
FILE * ifp;
ifp = fopen("input.txt", "r");
//Declare variables
char input[100][50]; //Array to hold the input numbers
int i, j; //Variables to work with loops
//Begin by reading in the input file as characters,
//otherwise fscanf will take each entire line as
//a single number
for (i = 0; i <= 99; i++)
{
printf("%d)", i);
for (j = 0; j <= 50; j++)
{
fscanf(ifp, "%c", &input[i][j]);
printf("%c", input[i][j]);
}
}
system("pause");
return 0;
}
This will print it correctly. The thing that seems weird to me, is that it doesn't actually need to use that extra row that solves the problem...fscanf still only functions for the same range as before (0-99).
So...why does the array need to be [100]x[50]? Why does [99]x[50] cause so many issues?
Also, I added a newline to the last line of the text file because if I didn't, instead of a newline it printed an apostrophe ' to the screen at the end of the last line. Is this the symbol for some sort of end of file character?
Thanks in advance!
By the way, if you're interested in compiling this and seeing it happen, here's the text file input.txt:
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690
So...why does the array need to be [100]x[50]? Why does [99]x[50]
cause so many issues?
100 is the size you allocate, the indices go from 0 to 99 (99 + 1 = 100 ).
When you only allocate 99 you're missing the last line.
Since you're using a C string to handle the fifty digits, you have to get 1 extra char to terminate the string.
char input[100][51];
for (int i = 0; i != 100; ++i) /* 100 entries */
{
/* read your 50 digits normally here */
input[i][50] = '\0'; /* remember to terminate the string */
}
For the C project coming up, the goal is to read in a CSV file with the first two lines listing the row and column lengths like
attributes: 23
lines: 1000
e,x,y,n,t,l,f,c,b,p,e,r,s,y,w,w,p,w,o,p,n,y,p
e,b,y,y,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,s,m
e,x,f,y,t,l,f,w,n,w,t,b,s,s,w,w,p,w,o,p,n,v,d
e,s,f,g,f,n,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,v,u
The thing is, I don't know if future file inputs will be of the same row/column lengths, so I'm implementing a determineFormat function to read those first two lines, which will be used for building the data structures.
In order to do this, I need to match a substring to the current line. If it matches, then fscanf is used to read in the line and extract the length integers. However, this code isn't working, as the entire strstr function is getting skipped over in ddd.
int lineCount, attrCount; //global variables
void determineFormats(FILE *incoming){
char *curLine= emalloc(CLINPUT);
int i;
char *ptr=NULL;
for (i=0; i<2; i++){
if (fgets(curLine, CLINPUT, incoming) != NULL){
ptr= strstr(curLine, "attrib"); //this line is skipped over
if (ptr!= NULL)
fscanf(incoming, "attributes: %d", &attrCount);
else
fscanf(incoming, "lines: %d", &lineCount);
}
}
printf("Attribute Count for the input file is: %d\n", attrCount);
printf("Line count is: %d\n", lineCount);
}
My thinking for the if/else block is since there are only two lines of interest to this function, and they're both at the head of the file, just scan each line and test if the string matches. If it does, then the non-null conditional is run, otherwise the other conditional is executed. However, in this case, the strstr function is getting skipped.
Extra Info
Some of the comments made me go back and double check.
CLINPUT is defined to be 100, or roughly 40% again the number of characters to read from each line.
This is the output from ddd when ptr= strstr(curLine, "attrib"); is called:
0xb7eeaff0 in strstr () from /lib/libc.so.6
Single stepping until exit from function strstr,
which has no line number information.
Once this happens, the line indicator disappears, and single stepping (F5) from that point returns to the calling function.
strstr is working good. Problem is that fscanf will read next line since current already read.
Here's more correct way
for (i=0; i<2; i++){
if (fgets(curLine, CLINPUT, incoming) != NULL){
if (strstr(curLine, "attributes:")) {
sscanf(curLine, "attributes: %d", &attrCount);
} else if (strstr(curLine, "lines:")) {
sscanf(curLine, "lines: %d", &lineCount);
}
}
}