Basic c programming help

Discussion in 'Mac Programming' started by Rudd999, May 10, 2013.

  1. Rudd999, May 10, 2013
    Last edited by a moderator: May 10, 2013

    macrumors newbie

    Joined:
    May 10, 2013
    #1
    hey guys, i dont get why i cant find a .txt file :( it so frustrating because i dont get why it cant find the file. i have tried it in all folders of the project and it just wont find it.

    im using c btw :p

    copy of the code:

    Code:
    {
    	char ch, ch2;
    	FILE *fp1, *fp2;
    	fp1=fopen("MyEncryptedFileA.txt","r");
    	if (fp1 == NULL)
    	{
            printf("ERROR no file\n");
            ch = getchar();
            exit(1);
    	}
    	printf("File Decrypted.... \n");
    	fp2=fopen("MyDecryptedFileA.txt","w");
        
    	ch = fgetc(fp1);
    	while(ch != EOF)
    	{
            /* decryption algorithm a here */
            ch2 = ch - 10;
            /* end of decryption algorithm */
            fprintf(fp2, "%c", ch2);
            ch = fgetc(fp1);
    	}
    	fclose(fp1);
    	fclose(fp2);
    	printf("\n**********************\n");
    	printf("\n\n");
    }
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I find file handling to be a rather complicated topic that shouldn't be taught early on, mostly because programmers rarely need to handle files.

    Having said that, what's the present working directory when your program is running?

    Are you using Xcode? If so, you need to make sure that in your Build Phases the file you'd like to access in your program is copied. To access Build Phases, click on your project in the navigator (left panel of Xcode,) then on your target (next panel to the right that opens when you select it,) then select the "Build Phases Tab". You might need to add a new build phase of the type Copy Files. Drag in the file that you want copied. The destination should be "Products Directory" if I recall correctly. Make sure "Copy only when installing" is unchecked.
     
  3. thread starter macrumors newbie

    Joined:
    May 10, 2013
    #3
    i dont know if this is going to help but thought it might show you what i have done so far :/ i have written the code myself and i understand how to write c but not how to link files in xcode
     

    Attached Files:

  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    Where is your "MyEncryptedFileA.txt" located? From the looks of it, not in your current working directory.
     
  5. thread starter macrumors newbie

    Joined:
    May 10, 2013
    #5
    the program creates the file. that is the point :p it reads the MyFile.txt file, ascii by ascii and adds 10 to the amount and then writes the new ascii to the new MyEncryptedFileA.txt file.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Right, you changed you code. Because your first version in post #1 sure attempts to opens the file for reading. Creating a caesar cipher, wouldn't you want to use any file as input?

    Still, you need to set your working directory to where the file is located (the file you can not open, previously MyEncryptedFileA.txt).
     
  7. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    Your cases don't match - that could be contributing to the issue. Your file (listed in the left sidebar) has camel case while in your code you have all lowercase letters. I'm not sure if fopen() is case sensitive but I'd say it's safer to guess it is and just match case just to rule that out as a potential issue.
     

Share This Page