work with files in С99

This is a discussion on work with files in С99 within the C Programming forums, part of the General Programming Boards category; Each line of text file contains the name of a file found in the current directory. Copy the file, removing ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    work with files in С99

    Each line of text file contains the name of a file found in the current
    directory. Copy the file, removing from it the names of non-existing files, and for
    each existing file, write down its size in bytes.

    My code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <io.h>
    
    int main (int argc, char * argv[])
    {
        FILE  *myFile, *newFile, *tmpFile;
        char *mas;
        char nameFile[100];
    char i;
    	int sizeFile;
        if (argc != 2)
            {
                printf ("Some arguments are skipped\n");
                exit (1);
            }
    if ((myFile = fopen (argv[1], "r+")) == NULL)
            {
                printf ("Can't open %s\n", argv[1]);
                exit (1);
            }
    newFile = fopen("new.txt", "r+");
    //fscanf (myFile, "%s", nameFile);
    
    mas = fgets(nameFile,100,myFile);   
    
    if (!mas)
    		printf("\n End file or error \n");
    	else 
    	    {
    	    tmpFile = fopen("\"" nameFile "\"","rb");
    		printf("%s", nameFile);
    		fseek(tmpFile,0,SEEK_END);
    		sizeFile = ftell(tmpFile);
    		fseek(tmpFile,0,SEEK_SET);
                fprintf(newFile, "File name %s File name %d", nameFile, sizeFile);   
    	    }
     
    
    return 0;
    
    
    }

    help please !

    test.txt :

    test.c
    test.h
    Last edited by Salem; 05-05-2010 at 02:53 PM. Reason: Adde [code][/code] tags - learn to use them yourself

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Please post code using CODE TAGS!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    We're all too lazy to press "quote" and then add some code tags ourselves, aren't we.

    Anyways, I stopped trying after I noticed this line:
    Code:
     tmpFile = fopen("\"" nameFile "\"","rb");

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by EVOEx View Post
    We're all too lazy to press "quote" and then add some code tags ourselves, aren't we.
    You mean "pressing quote, then adding code tags, then manually indenting the thing ourselves, because non-code-tagged-code doesn't preserve whitespace, which is why we b... about not having code tags in the first place.

    There, fixed that for you.


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

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by quzah View Post
    You mean "pressing quote, then adding code tags, then manually indenting the thing ourselves, because non-code-tagged-code doesn't preserve whitespace, which is why we b... about not having code tags in the first place.

    There, fixed that for you.


    Quzah.
    Actually, when you press "quote" is preserves whitespace just fine. It just won't show up in the rendered page. So, no, I was correct. I even pressed "quote" [when I posted the previous message] to check - the code was indented properly.

    Edit: makes pointer from integer without a cast . QED ;-).
    Last edited by EVOEx; 05-06-2010 at 05:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  2. How do you work with resource files???
    By Finchie_88 in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2004, 02:39 PM
  3. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 01:51 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 04:18 PM

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