Thread: Is this reading file true?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    17

    Is this reading file true?

    I am programming in visual studio 2010 and
    want to read some text files (page1.txt page2.txt ...)(that each has some characters and numbers),that their names are in the other text file(gamemap.txt)
    at first i open gamemap.txt
    Code:
    FILE* ptr_map;
        ptr_map=fopen("gamemap.txt","r");
    then want to read lines one by one (each line is something like page6.txt)
    Code:
    fscanf(ptr_map,"%s",current->page);
    that Page* current=(Page*)malloc(sizeof(Page));

    and Page is:
    Code:
                           typedef struct PAGE{
    
                            char page[10];
                            shape* element;
                            struct PAGE* next;
                            struct PAGE* previous;
                           } Page;
    after reading the first line i want to put(or copy)this string in an array that is belong to a structure and then open the corresponding text file and then read it.
    Code:
    FILE* ptr_page=fopen(current->page,"r");
    see these pictures

    is it correct?
    Attached Images Attached Images Is this reading file true?-gamemap-png Is this reading file true?-page6-png 

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No. fscanf() won't read in an entire line of text. It will stop reading in, when the first word ends.

    Use fgets(buffer, sizeof(buffer, filePointer). THAT will read in an entire line of text. And this, will read in the entire text file, line by line:
    Code:
    char buffer[100];
    FILE *fp;
    
    //open the file here, and test if it opened OK also.
    
    
    
    while((fgets(buffer, sizeof(buffer), fp)) != NULL) {
       //all your code to do what you want with this line of text
    
    }
    
    fclose(fp); //the file has been read, so close it.
    If you need to grab a corresponding file name, then use strstr(), to find it for you, within the buffer, as the while loop iterates. You will need to include string.h in your program to use strstr().

    Be aware that fgets() will keep a newline at the end of any line (space permitting), into the buffer, as well as an end of string char marker: '\0'.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    17
    I did but you can see what happened!
    Attached Images Attached Images Is this reading file true?-3-png 

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It appears from your pic that page[] needs to be quite a bit bigger to handle all the whitespace (which fgets() will be saving).

    Isn't current->page a pointer to page, so you don't want sizeof() on just the pointer?

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    17
    i incremented the size of current->page (1000) but that error happened again.????????what should i do?


    and if that you said about pointer... when you identify buffer : char buffer[100]; so <buffer> is a pointer too. so i did something like you(sizeof ( a pointer). what's the problem?

    help!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mathink View Post
    i incremented the size of current->page (1000) but that error happened again.????????what should i do?


    and if that you said about pointer... when you identify buffer : char buffer[100]; so <buffer> is a pointer too. so i did something like you(sizeof ( a pointer). what's the problem?

    help!
    sizeof() is calculated at compile time. At compile time, buffer[] had a known (and large) size - it was NOT just the size of a pointer.

    Not so with the pointers whom you allocate memory to, at run time. They would be given the size they had at compile time, which is usually 2 or 4 bytes, only.

    Print out the sizeof() each, and see for yourself. Note that sizeof() an array, works in main(). After the array is passed as a parameter to another function, it "decays" to the size of a pointer. At that time, you need to pass the sizeof() the array, into the function that you have called.

    After you fix the first problem, what do you do next? You fix the second problem.

    'Cause you is da Fixer!

    There may be other problems, but if so, we'll get to them.
    Last edited by Adak; 01-16-2013 at 03:06 AM.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    17
    ok

    i corrected it.

    thanks so much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  2. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  3. Replies: 21
    Last Post: 02-06-2011, 11:01 AM
  4. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM