Opening a text file in C

This is a discussion on Opening a text file in C within the C Programming forums, part of the General Programming Boards category; I have a program that is to open a text file. The code compiles, appears to validate as it should ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    12

    Opening a text file in C

    I have a program that is to open a text file. The code compiles, appears to validate as it should and so on....except:

    If I used this code to open the file:

    bank = fopen("C:\\tma307\\itembank.txt", "r");

    it opens just fine.

    but if I use:

    bank = fopen(Bank, "r");

    it won't open (I get a popup error that reads "Project1 has caused an error in CC3260MT.DLL. Project1 will now close")

    Bank is the itemfile and bank is the pointer for my program.

    What hapens is:

    --I click on run (using Borland C++ Builder)

    --It brings me to a menu in dos and prompts me to select an item

    --I select v (for validate file) and it brings me to a blank dos screen where I am prompted to type in the disk file name.

    --This is where I first noticed the problem. I don't even have to type in a disk file name to get my text file. I could just hit enter or happy new year for that matter.

    I believe that as I have already declared the disk file name in the code, the program already knows where to go. But, if I make changes to the text file (and save them of course), it doesn't recognize anything different.

    I have read many many articles on fopen etc. to try and figure this one out but have been unsuccessful.

    Any suggestions would be greatly appreciated!

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    FILE * bank
    bank = fopen("C:\\tma307\\itembank.txt", "r");

    Should open the file for reading, so you may do whatever to it in your program later on.

    but if I use:

    bank = fopen(Bank, "r");

    it won't open (I get a popup error that reads "Project1 has caused an error in CC3260MT.DLL. Project1 will now close")
    Of course you do. You aren't opening anything.

    > bank = fopen(Bank, "r");
    The only way this would work was if you were to do:
    FILE * bank;
    Bank = "C:\\tma307\\itembank.txt";
    bank = fopen(Bank, "r");
    Which confuses me just looking at it.
    ( then again, it's late )

    What exactly are you looking to do?
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    12
    Thanks for replying Shadow. Yes it is late and yes it is confusing.

    I am opening the text file to validate certain conditions within the text. For example if a line reads more than 64 characters in length, return error reading "file length is too long - edit line length...".

    So I believe that the correct way to open this file to validate it is:

    FILE * bank;
    bank = fopen(Bank, "r");

    but it will only let me open the file with:

    FILE * bank;
    bank = fopen("C:\\tma307\\itembank.txt", "r");

    I don't understand why I have to specify the path address there. I thought that I might have done something in main but all is in order there (I have compared my main.c with another classmate and they are exactly the same.

    Perplexing isn't it!

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    12
    Thanks for reply Salem. I agree that better variable names would have been ideal however they are part of my assignment of which I cannot change. I think they do that to keep us on our toes.

    Anyway, here is more code:

    int validate_file ( const char Bank[], int D_A[], int summary )
    {
    FILE * bank; // file pointer variable to item-file Bank

    int err,
    ....

    clrscr ();
    bank = fopen("C:\\tma307\\itembank.txt", "r"); //

    if...
    {
    printf("Error 7: ...");
    ...

    hit_a_key("\n\n\n");

    for( i=0; i!= EOF; ++i)
    {
    ...and so on.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Given that function,
    bank = fopen(Bank, "r");
    seems OK

    So the only question is
    How are you calling this function?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    12
    I'm calling it as follows:

    if(validate_file ( Bank, D_A, summary ) )
    {
    (bank != NULL)
    {
    printf("Error 7: Unable to open itembank file.\n");
    printf("try to use the full name: C:\\TMA307itembank.txt\n");
    }
    else if (bank==NULL)
    {
    printf("The itembank file you have requested has opened successfully");
    }
    hit_a_key("\n\n\n");

    for( i=0; i!= EOF; ++i)
    {
    ...

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > (bank != NULL)
    This bank is not the same as the local variable bank in the validate_file function.

    This bank is uninitialised

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    12
    Sorry Salem, I missed a part of the code when I was pasting it. It should read:

    if (bank != NULL)
    ...

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Perhaps if you posted all your code, not just random snippets

    Besides, doesn't matter how you wrote it, you still have a scope problem

    In validate_file
    bank = fopen("C:\\tma307\\itembank.txt", "r"); //

    This is NOT the same bank as the one in the caller of validate_file

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    12
    Yes, I agree that I have a scope problem. That's why I didn't want to post the whole code (it is very long).

    My assignment specs state "Bank is the disk-name of an item-file to be validated"

    So if I state Bank=fopen I get the following compiler error:

    "can't modify a const object"

    I know it must be quite difficult to give me advice when all the project facts are not know. Any suggestions on where I might get further info on "scope" issues or what else I might search under? I am sure that I have reviewed most of the sites on fopen etc.

    Thanks

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Perhaps it should be like this
    Code:
    if ( validate_file ( Bank, D_A, summary ) ) {
      bank = fopen(Bank, "r"); 
    }

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    12
    Thanks Salem but I tried that and it won't open the file for me. I just get a blank screen (yes I checked to make sure that there was indeed data in the text file).

    Any other suggestions? Anyone?

    I know there's a genius out there with the answer. Or any suggestions on where to look (ie a tutorial etc)?

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Apart from the scope problem Salem has mentioned,

    Can you use a break point?

    If so check the contents in the string in the fopen() call. Make sure you are not trying to open

    C:\\tma307\\itembank.txt
    but
    C:\tma307\itembank.txt

    could be having a problem with the '\t' (which is a horz tab)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int validate_file ( const char Bank[], int D_A[], int summary )
    {
    So given this...

    So if I state Bank=fopen I get the following compiler error:

    "can't modify a const object"
    You then do that?

    Well no **** you can't modify 'Bank'. You've defined it as 'const char Bank[]'. Furthermore, if in fact you're doing it in this same function, 'Bank' is a character array. It is not a file pointer. FURTHERMORE, you can't assign ANYTHING to an array (other than a single element at a time) with the equals sign.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > That's why I didn't want to post the whole code (it is very long).

    That's why there's a box at the bottom of the reply window called

    "Attach file:"

    I would suggest you use it.

    I'm worried by the apparent size of this program, and the lack of understanding of some basic C programming principles.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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