Jump to content

I have a logical error here D:

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts

#include<stdio.h>

#include<stdlib.h>

void saveem()

{

    system("cls");

    char buffer[80];

    FILE *fp, *fp2;

    int c;

    fp=fopen("C:\Users\Moudi\Desktop\hahaaoida.txt", "a");

	fp2=fopen("C:\Users\Moudi\Desktop\hahaaoida2.txt", "a");

    fgets(buffer, 80, stdin);

    fprintf(fp, "%s", buffer);

	system("cls");

	fclose(fp);

    while ((c=fgetc(fp))!=EOF)

    {

        fputc(c+4, fp2);

    }

    printf("Done!\n");

	fclose(fp);

	fclose(fp2);

}

void deleteem()

{

    remove("C:/test.txt");

}

void showem()

{    

    int a;

    char character;

    FILE *file,*file2;

    file=fopen("C:/test.txt", "rb");

    file2=fopen("C:/test2.txt", "wb");

    while ((a=fgetc(file))!=EOF)

    {

        fputc(a-4, file2);

    }

    fclose(file);

    while ((character=fgetc(file2)) != EOF) {

    putchar(character);

    }

    fclose(file2);

    remove("C:/test2.txt");

}

int main()

{

    char buffer2[20];

    int input;

    printf("1.Write something in test.txt");

    printf("\n2.Delete test.txt\n");

    printf("3.Show the content of test.txt\n");

    scanf("%d", &input );

    switch (input) {

        case 1:

            saveem();

            break;

        case 2:

            deleteem();

            break;

        case 3:

            showem();

            break;

        default:

            printf(" Bad input, Quitting!\n");

            break;

    }

    getchar();

    return 0;

}

So what's happening here if i input "1" it will only show "Done" without asking for the buffer and it won't write it to the file specified.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Are you sure that fp actually points to a file, rather than being null?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Yea i accually created the file also ( the program should create it by itself )

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You aren't checking fp to ensure it isn't null before writing to it. You may want to dump buffer to screen as well, just to ensure that it was read correctly.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Using scanf for user input nearly always ties newbs in knots. I therefore generally recommend taking all user input as strings and converting to numeric if necessary.

A short explanation is that when you respond to the request for numeric input, you press enter. This value -- the newline -- is not discarded but remains in the input buffer. Mixed with a call for string data, the newline will immediately satisfy the call without pausing for user input. So if you don't take the user input as a string as I earlier suggested, then you will have to clean up the input stream to get rid of the newline.

#6
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I'm really bad at this, Can someone clean it up for me and explain a little ? ( Try staying away from hard language :P )

#7
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Instead of this:
fp=fopen("C:\Users\Moudi\Desktop\hahaaoida.txt", "a");

fp2=fopen("C:\Users\Moudi\Desktop\hahaaoida2.txt", "a");

Try this:
fp=fopen("C:\\Users\\Moudi\\Desktop\\hahaaoida.txt", "a");

fp2=fopen("C:\\Users\\Moudi\\Desktop\\hahaaoida2.txt", "a");

Backslashes are interpreted as escape characters, so you need two backslashes instead of just one. Also, you should always check to see if your files opened correctly. If the file pointer is NULL, print an error and exit with failure:
if (fp==NULL) {

    printf("Error opening file!\n");

    return 1;

}

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Alternatively, use forward slashes.