problem with open file.

This is a discussion on problem with open file. within the C Programming forums, part of the General Programming Boards category; Hi, i have a problem. i have a file that there is a line in the file : #include "hello.h" ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    14

    problem with open file.

    Hi, i have a problem.
    i have a file that there is a line in the file : #include "hello.h"
    i have to open the "hello.h" file and copy it to another file.
    i did :

    char getline[MAX_LINE];
    char *fp1;


    fp1 = strchr(getline2,'#'); /* fp1 = #include ..., taking the include from the line */
    fp1 = strchr(fp1,'"'); /* fp1 = "hello.h" */
    FILE *file = fopen(fp1,"w");

    the file hello.h is in the main libary and the file not opened.

    i hope i was understandable, and if someone can help me i`ll appreciate it.
    Thanks,
    Or.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you intend to open it for writing? If not you should have used fopen(fp1,"r");

    Also you should check the return value from fopen() to make sure it succeeded.
    Then, of course, you will need to read the fine in line by line or character by character to display it.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    14
    its for writing, and my problem that the file cant open.
    so its not succeeded, thats what i want to know,
    why its not succeeded.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Because the value of fp1 will be "hello.h", not hello.h. strchr returns a pointer to the first occurrence of the character, which will be ". Your OS is likely balking at creating a file with quotes in it.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    14
    Wow, you so helped me!
    Thank you very much!!!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Because the value of fp1 will be "hello.h", not hello.h. strchr returns a pointer to the first occurrence of the character, which will be ". Your OS is likely balking at creating a file with quotes in it.
    Nice catch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 06:38 PM

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