-1

I am using fscanf to read from a file, and I am trying put those strings into an array. It seems to work but I'm only getting the last string into my array. When i try to print it, I'm only print the characters of my last string. I didn't include my method because it doesn't effect what fscanf does in case anyone got confused. Also, my CMDLINE file has the following strings foo barr tar 526-4567 456-8792.

Output is :

456-8792
56-8792
6-8792

etc...

2

Here is the code:

int main (int argC, char *argV[]) {
    FILE *fp;
    int index;
    int ret;
    char str[1000];

    //Need at least 2 files to begin program
    if (argC < 3) {
        fprintf(stderr, "Usage: %s file\n", argV[0]);
        exit(1);
    }//if statemtn

    //check to see if the CMDLINE file is in the arguments
    ret = scanC(argC, argV);

    //if no CMDLINE file is found, print error and exit
    if (ret == 1) {
        fprintf(stderr, "you must provide a CMDLINE file\n");
        exit(1);
    }

    //iterate and open CMDLINE file and read from it
    for (index = 0; index < argC; index++) {
        if (strcmp(argV[index], "CMDLINE") == 0) {
            fp = fopen(argV[index], "r");
            //error check
            if (fp == NULL) {
                fprintf(stderr, "Counld not open file %s\n", argV[index]);
                exit(1);
            }//if statment

            //read from fscanf and put it's arguments into an array
            while (!feof(fp)) {
                char *p2 = str;
                //scan the strings of the file into str array
                while (fscanf(fp, "%s", p2) != EOF) {
                    p2++;
                }//while loop 2
            }//while lop 1

            //close the file for it is not needed to be open anymore
            fclose(fp);
        }//if statement
    }//for looop

    char *p;
    p = str;
    int j;
    for (j = 0; j < strlen(str); j++) {
        printf("%s\n", p);
        p++;
    }
    return 1;
}
1

2 Answers 2

2
char *p;
p = str;
int j;
for (j = 0; j < strlen(str); j++) 
{
    printf("%s\n", p);
    p++;
}

You have one string, example "abcd", you can just print it as printf("%s\n", str); Instead you are printing the same string starting at different offsets. Result is as follows:

abcd
bcd
cd
d

Perhaps you are confused between "character array" and "array of strings"

//This will reserve 100 character arrays, or 100 strings
char *arr[100];

int count = 0;
while (fscanf(fp, "%999s", str) == 1)
{
    arr[count] = malloc(strlen(str) + 1);
    strcpy(arr[count], str);
    count++;
    if (count == 100)
        break;
}

int i;
for (i = 0; i < count; i++) 
    printf("%s\n", arr[i]);

In real world application you use malloc and realloc to allocate array of strings large enough to read the file.

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

Comments

1

you can use a marker to set the "current character":

char *p2 = str;
size_t k = 0;
size_t pos = 0;
//scan the strings of the file into str array at position pos
while((k = fscanf(fp, "%s", p2 + pos)) != EOF){
    // update pos
    pos += k;
    p2++;
}//while loop 2

This will enable to store the whole file in your string PROVIDED IT IS LESS THAN 1000 CHARACTERS! otherwise just add a safeguard in the while loop.

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.