Like Tree1Likes
  • 1 Post By CommonTater

Problem while opening a file !

This is a discussion on Problem while opening a file ! within the C Programming forums, part of the General Programming Boards category; I have a file names 'bubble sort and print a fragment.c' The destination of the file is - C:\Users\RAJARSHI\Desktop\C programming ...

  1. #1
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79

    Problem while opening a file !

    I have a file names 'bubble sort and print a fragment.c'
    The destination of the file is -
    C:\\Users\RAJARSHI\Desktop\C programming files\bubble sort and print a fragment.c

    I want to open the file using C

    I wrote these following codes :
    But they are not opening the file....Is there something wrong with the codes ?? I am new to File I/O. Please help
    Code:
    #include <stdio.h>
    int main ()
    {
        FILE *fp;
        char ch;
        fp=fopen("C:\\Users\RAJARSHI\Desktop\C programming files\bubble sort and print a fragment.c","r");
        while (1)
        {
            ch = fgetc(fp);
            if (ch == EOF);
            break;
            printf ("%c",ch);
        }
        printf ("\n");
        fclose(fp);
        return 0;
    }

    and

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
        FILE *fp;
        char ch;
        fp=fopen("C:\\Users\RAJARSHI\Desktop\C programming files\bubble sort and print a fragment.c","r");
        if (fp==NULL)
        {
    puts ("cannot open file");
     exit(1);
        }
    
    
        return 0;
    }

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > fp=fopen("C:\\Users\RAJARSHI\Desktop\C programming files\bubble sort and print a fragment.c","r");
    You need \\ on ALL your directory components.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Salem View Post
    > fp=fopen("C:\\Users\RAJARSHI\Desktop\C programming files\bubble sort and print a fragment.c","r");
    You need \\ on ALL your directory components.
    when I used
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
        FILE *fp;
        char ch;
        fp=fopen("C:\\Users\\RAJARSHI\\Desktop\\C programming files\\bubble sort and print a fragment.c","r");
        if (fp==NULL)
        {
    puts ("cannot open file");
     exit(1);
        }
    
    
        return 0;
    }
    and
    Code:
    #include <stdio.h>
    int main ()
    {
        FILE *fp;
        char ch;
        fp=fopen("C:\\Users\\RAJARSHI\\Desktop\\C programming files\\bubble sort and print a fragment.c","r");
        while (1)
        {
            ch = fgetc(fp);
            if (ch == EOF);
            break;
            printf ("%c",ch);
        }
        printf ("\n");
        fclose(fp);
        return 0;
    }
    The output was a blank screen in both the cases..it didn't open the file !!
    Last edited by rajarshi; 11-06-2011 at 07:11 AM.

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    if (ch == EOF);
    break;
    Your if statement does nothing, so your loop ALWAYS breaks on the first iteration, having written nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Salem View Post
    if (ch == EOF);
    break;
    Your if statement does nothing, so your loop ALWAYS breaks on the first iteration, having written nothing.
    Thanks a lot sir !!

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... you're on windows... Open the following chain...

    Start -> Control Panel -> Folder Options -> View

    then remove the checkmark from "Hide extensions for known file types".

    And OK your way out.

    You may well discover your file is "c:\users .... fragment.c.txt" or something similar.
    (This, by the way, is one of the dumbest ideas Microsoft ever came up with! You just don't hide information from a user.)


    Also... it's a real bad idea to give known programming language extensions to data files... way too much chance for weirdness.

    Next, you should not put files like that on your desktop. You may end up with a shortcut to the file, not the file itself and C doesn't natively know how to follow shortcuts.

    Finally... some older compilers have trouble opening files with spaces in their names, which is a hangover from the long gone 8.3 filename days.
    Last edited by CommonTater; 11-06-2011 at 07:36 AM.
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening file problem, encoding?
    By plan7 in forum Windows Programming
    Replies: 4
    Last Post: 11-30-2009, 05:56 AM
  2. problem in opening a txt file
    By arian in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 01:17 PM
  3. file opening problem
    By bigmac(rexdale) in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2008, 11:57 PM
  4. strange file opening problem
    By ac251404 in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2006, 04:35 PM
  5. problem re-opening file
    By Markallen85 in forum C Programming
    Replies: 3
    Last Post: 06-12-2003, 11:02 AM

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