#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Help MAC .txt file reading problem


    For some reason when I am trying to read something from a file in the source folder. I am getting random data that is nothing like the file I am suppose to be reading from. Can anyone shed some light.

    This is the input.text holds the names, student id and grades. Below that is the gibberish I am getting from xcode. Why is what it is reading nothing like what the code should be.
    003141592 Carl A
    002345678 Bob B
    007766554 Dorothy C
    001234567 Alice A
    5 1 . 9 0 0 0 0 0
    p
    1 0 1 5

    #include <stdio.h>
    int main()
    {
    FILE *fptr;
    char h;
    fptr = fopen("input.txt","rt");
    h = getc(fptr);
    while (h!=EOF)
    {
    printf("%c", h);
    h = getc(fptr);
    }
    fclose(fptr);
    return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    I just tried your code and it worked correctly. What is the mode for fopen "rt"?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    getc return a value of type int. You are assigning that value to an object of type char, therefore losing some of its properties.

    I suggest you define h as being of type int.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Int


    As an int it returns the same thing which makes sense since it would appear to be pointing to the wrong file and reading the wrong data altogether.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    rt


    rt being read text from what I understand.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Angry Tried


    I was googling away and came with another approach of getting the same thing. Provides me again rather than the intend data.

    5 1.900000
    p

    10 15

    #include <stdio.h>
    int main() {

    FILE *fp;
    int c;

    if ((fp = fopen ("input.txt", "rb")) == NULL)
    return -1;

    while ((c = fgetc(fp)) != EOF)
    fprintf(stdout, "%c", c);

    fclose(fp);
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Are you sure your opening the correct file?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Angry The file


    I have the file in the folder. I even tried making sure to add file through xCode. I can even opent eh txt in xcode and see what txt has in it since it shows up in the project directory on left panel. However, the data within the .txt is not the data I am getting for fgetc.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by neuroh
    I have the file in the folder. I even tried making sure to add file through xCode. I can even opent eh txt in xcode and see what txt has in it since it shows up in the project directory on left panel. However, the data within the .txt is not the data I am getting for fgetc.
    Did you try opening the input file using the full pathname + filename?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Smile Thanks


    Actually that was spot on even though technically adding it to the folder should of made it so I did not have to specify the location. Hmm, it is weird that it didn't work for me. Thanks you, for that, it did the trick. :)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by neuroh
    Actually that was spot on even though technically adding it to the folder should of made it so I did not have to specify the location. Hmm, it is weird that it didn't work for me. Thanks you, for that, it did the trick. :)
    When you add something to a project, it doesn't necessarily add it to the same folder as the exe.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    =/


    True enough but I didn't just added to project I also put it into the project directory.

IMN logo majestic logo threadwatch logo seochat tools logo