0

I am having some trouble with some code to load from a line separated text file that is formated like this:

aa
bb
dd
ccasdf
dfdsafefasd
vdasfeadsaf
cvdasegfdjasflfe
swedtd

To an array, I simply want to end up with an array containing everything that was in the text file... Here is my code:

#include<stdio.h>
#include<string.h>


int main()
{
    int size = 0;
    char peeps[10000][50];
    FILE *people = fopen("test.txt","r");
    while(fscanf(people,"%s",peeps[size]) != EOF)
{
    size++;
}
fclose(people);
return 0;
}

When I run it I get a segfault, I have looked over what gdb spits out, but I can't figure out how to fix it... Here is what gdp prints out:

Program received signal SIGSEGV, Segmentation fault.
_IO_vfscanf_internal (s=s@entry=0x804a008, format=format@entry=0x804865f "%s",     argptr=argptr@entry=0xbff85298 "4X\305I", 
    errp=errp@entry=0x0) at vfscanf.c:1073
1073                  *str++ = c;
(gdb) bt
#0  _IO_vfscanf_internal (s=s@entry=0x804a008, format=format@entry=0x804865f "%s", 
    argptr=argptr@entry=0xbff85298 "4X\305I", errp=errp@entry=0x0) at vfscanf.c:1073
#1  0xb7e5564d in __isoc99_fscanf (stream=0x804a008, format=0x804865f "%s") at     isoc99_fscanf.c:35
#2  0x08048581 in main () at fdsa.c:10

Anyone know what is going wrong???

2
  • In gdb when it crashes, can you go up the call stack to main and print size? Commented Apr 13, 2013 at 5:08
  • I did that and this is what it outputted: $1 = 1936023908 Commented Apr 13, 2013 at 5:11

2 Answers 2

2

Anyone know what is going wrong???

I. Three consecutive question marks.

II. scanf(people,"%s",peeps[size]) != EOF) - if all you want is reading a file into memory, then never, ever use scanf() for that.

III. Don't use int for sizes. Use size_t, there's a good reason it's in the standard library.

FILE *f = fopen("foo.txt", "r");
size_t total_size = 0;
size_t num_bytes = 0;

char buf[0x1000];
char *result = NULL;

while ((num_bytes = fread(buf, 1, sizeof(buf), f)) > 0) {
    char *p = realloc(result, total_size + num_bytes);
    if (p == NULL) {
        fclose(f);
        free(result);
        abort(); // whatever, out of memory
    }

    result = p;
    memcpy(result + total_size, buf, num_bytes);
    total_size += num_bytes;
}

fclose(f);

If you ever want to be a bit more efficient, implement logarithmic expansion of the dynamic buffer.

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

6 Comments

when I try to run your code, I get this error: error: use of undeclared identifier 'tmp'
@JoshuaMcDonald: I think tmp and buf were mixed up.
@JoshuaMcDonald man gcc to fix all compiler errors (yes, you should be able to do that yourself!), but anyway it's good now.
oh, maybe not! I tried adding this code at the end to print out the array: printf("%s\n",result); And It printed NOTHING, there was just a blank line... Am I doing the printing wrong or is there something else not quite right?
sorry, I am new at this, I don't actually understand what you are saying :S
|
1

Try changing your test from != EOF to > 0.

The standard (§7.19.6.2 ¶16 of draft N1256, for the curious) states that:

The fscanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

I suspect that your C standard library is not interpreting an EOF as an error condition, so it's not returning EOF, but rather zero.

I should note that the examples in the standard all use feof and ferror rather than checking the return value of fscanf for checking for end of file.

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.