newbie reading a file..how? plz help

This is a discussion on newbie reading a file..how? plz help within the C Programming forums, part of the General Programming Boards category; hi i have to read a file(pj.dat)in 3 different ways. 1. file is in the same dir as the exe ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    newbie reading a file..how? plz help

    hi i have to read a file(pj.dat)in 3 different ways.
    1. file is in the same dir as the exe and i think it will be read by doing this:

    FILE *fpData;
    fpData=fopen("pj.dat","r");

    2. i have to read the same file as it was in "C:" Drive i.e "C:\pj.dat"
    for this i think we just supply the path which would look like:

    FILE *fpData;
    fpData=fopen("c:\pj.dat","r");

    3. reading the same file from any drive letter(or directory structure) input by the user.
    I am not sure but i think it should be something like:

    char filePath[30]; //max chars of the filepath
    FILE *fpData;
    printf("Please enter the path:");
    scanf("%s",filePath);

    fpData=fopen(filePath,"r");


    i am not sure if these will work. Can anyone make sure and point out my mistakes if i am wrong???

    thanks
    fahad

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>fpData=fopen("pj.dat","r");
    dont forget to check out if you opened succefully the file, look:
    Code:
    if ((fpData = fopen("pj.dat","r")) == NULL) {
        //error, couldn't open the file
    } 
    
    else {
       //now do your code.
    }
    >>scanf("%s",filePath);
    for getting strings from a stream, use fgets, and don't forget to check if you have '\n' (fgets when can put, it put) in the string example:
    Code:
    char *p, filePath[SIZE];
    ...
    fgets(filePath,sizeof(filePath),stdin);
    p = strchr(filePath,'\n');
    if(p) *p = 0;
    For using the strchr() function add string.h

    Now for reading, use function like, fgets(), fscanf(), fread() and so on... and for writing fprintf(), fwrite()...

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Be aware that a backslash in a character constant or a string literal introduces an escape sequence. Common examples are '\n' for a newline and '\t' for a tab.

    So for #2, your example is not correct.
    Code:
    FILE *fpData=fopen("c:\pj.dat","r");
    To have a backslash in the string literal, it would be coded as follows.
    Code:
    FILE *fpData=fopen("c:\\pj.dat","r");
    Note that this is for string literals as in your case #2. You wouldn't need to enter two backslashes for user input as in case #3. That is to say at the command line you could do something like the following.
    Code:
    Please enter the path: c:\pj.dat
    Last edited by Dave_Sinkula; 04-09-2003 at 12:58 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21