0

I've been searching for a while and can't figure out my problem. What I'm trying to do is read in a .txt file full of keywords separated by commas. Then I wanted to add each individual keyword into its own index for an array to be able to access later.

I have been able to print the .txt file as is, but I can't figure out how to add the whole string of each word into the array instead of the individual characters. This array is to be used to search another .txt file for those keywords. So to clarify:

.txt file that is read in:

c, c++, java, source,

What the array looks like now

f[0]c
f[1],
f[2]c
f[3]+
f[4]+
f[5],
f[6]j
f[7]a
f[8]v
f[9]a
etc

What i'm looking to accomplish:

f[0] = c
f[1] = c++
f[2] = java
f[3] = source
etc

It was for an assignment that I couldn't finish the way I wanted. I am only curious to find out what I would need to start looking into because I think this is something a little above my current level in class. Below is the code that I have made up to print the .txt file into an array. Any information would be awesome. I haven't learned memory allocation or anything yet and this was mainly to learn about FILE I/O and search functions. Thanks again!

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#define pause system("pause")
#define cls system("cls")
#include <string.h>


main(){
FILE* pFile;
char f[50] = {""};
int i = 0;

pFile = fopen("a:\\newA.txt", "r");
if (!pFile) {
    perror("Error");
}

    fread(f, sizeof(char), 50, pFile);
    printf("%s\n", f);

pause;


}
4
  • Your array is of individual characters. There is no getting around that. If you want to do something different you can. If all you want is to dump line-by-line words separated by commas with whitespace stripped, you don't need an array at all. simple example. Commented Oct 11, 2014 at 21:28
  • Thank you for the response WhozCraig. I'm looking to use these keywords to search another .txt file for the same words, then output the amount of times they were found. So I was thinking to add the actual string to each index of the array instead of each individual char. Like I was saying I'm not sure if this is more advanced than where I'm at currently considering it's an early C class. Again thank you for your help. Commented Oct 11, 2014 at 21:37
  • to add the actual string to each index of the array = you're not going to do that with that array of char. You'll need a dynamic array of char*. There are ways to do it, including reading the file en'masse into a single properly sized buffer, then walking it with a strtok loop delimited on " ," and building a dynamic char* array, but I can almost guarantee you that is a few weeks down the road in your class. Commented Oct 11, 2014 at 21:40
  • I kind of figured that was the case. I saw a lot of information about strtok but didn't make sense due to the malloc etc. So It's to be assumed that I would just allocate a large number for the char array to accommodate the list of strings? Again I really appreciate your input on the matter. You kept me from pulling my hair out. Commented Oct 11, 2014 at 21:45

2 Answers 2

1
char f[50] = {""};

This line means you build an empty array of 50 characters. Each f[i] will contains 1 and only 1 character. I give you this code that can print what you want but not sure if it is what you're asked to do...

main(){
FILE* pFile;
char f[50] = {""};
int i = 0;

pFile = fopen("a:\\newA.txt", "r");
if (!pFile) {
    perror("Error");
}

fread(f, sizeof(char), 50, pFile);
for(int j = 0; j<50; j++) {
    if(f[j] == ',') {
        printf("\n");
    } else { 
        printf("%c", f[j]);
    }
}
pause;

}

This will print your words, delimited by ','... but your array will remain the same !

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply Riberic! That is what I was looking to do for the first part. I don't know if that's a thing to have each of the words in their own index of one array. I'm continuing to research with no luck.But I can at least see how you were able to eliminate the commas out of the output. Thanks again!
Consider changing main() (which is very Old School) to the modern int main (void), and return an appropriate exit value at the end. Also, consider adding an error check for the fread, and use fclose when done with a file. The earlier you learn to always do so, the better!
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *getWord(FILE *fp, const char *delimiter){
    char word[64];
    int ch, i=0;

    while(EOF!=(ch=fgetc(fp)) && strchr(delimiter, ch))
        ;//skip
    if(ch == EOF)
        return NULL;
    do{
        word[i++] = ch;
    }while(EOF!=(ch=fgetc(fp)) && !strchr(delimiter, ch));

    word[i]='\0';
    return strdup(word);
}

int main(void) {
    char *word, *f[25];
    int i, n = 0;
    FILE *pFile = fopen("a:\\newA.txt", "r");

    while(word = getWord(pFile, ", \n")){
        f[n++] = word;
    }
    fclose(pFile);

    for(i=0; i<n; ++i){
        puts(f[i]);
        //free(f[i]);
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.