Thread: help me with saving to external file by character

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    help me with saving to external file by character

    hello guys

    i want to ask a question
    i am newbie in C proggramming

    so, this is my code

    Code:
     #include<stdio.h>
                    #include<stdlib.h>
                    #include"boolean.h"
                    
                   
                    boolean ceksama(char c[20])
                    {
                                       
                      FILE *f;
                      int retval;
                      boolean same=false;
                      char ccarray[20];
                      int i,j;
                      char cc;
    
                    f=fopen("customerfile.txt","r");
    
                    j=0;
                    while(c[j]!='\0')
                     {
                        j=j+1;
                     }
    
                    retval=fscanf(f,"%c",&cc);
                    while((retval!=EOF)&&(!(same)))
                     {
                        i=1;
                        while(cc!='\0');
                          { 
                             retval=fscanf(f,"%c",&cc);
                             ccarray[i]= cc;
                             i=i+1;
                          }
    
                        if(i!=j)
                          {
                              same=false;
                          }
                        else
                          {
                              if(ccarray[i]=c[j])
                                    {
                                    same=false;
                                }
                              else
                                {
                                     same=true;
                                }
                          }
                    }
    
                    fclose(f);
                    return same;
                    }
    
                    
    
                    int main()
                    {
    
                      FILE *f;
                      char nama[20];
                      int retval;
                      char c;
                      int i=1;
    
    
                    /*algoritma*/
                    f=fopen("customerfile.txt","a");
    
                    printf("\nenter your name:");
                    scanf("%c",&c);
    
                    /*
                    if (!(ceksama))
                    {*/
    
                    while (c=='\n')
                      {
                         scanf("%c",&c); 
                      }
                       
                                    while (c!='\n')
                      {
                          nama[i]=c;
                          i=i+1;
                          scanf ("%c",&c);
                      }
                    nama[i]=c;
                    i=1;
                    
                                    while(nama[i] != '\n')
                      {
                          retval=fprintf(f,"%c",nama[i]);
                          i=i+1;
                      }
                    fprintf(f,"%c",'\0');
                    fprintf(f,"%c",'\n');
                    
                    /*}
                    else
                      {
                          printf("data has been registered");
                       }*/
                    fclose(f);
    
                    }
    i hope you can see my code clearly
    in my code, i just want to input a name and save it to external file,
    in case, when inputting name, the program will check it first, is it the name are already exist in external file or not

    so, main is my main program, and above it the function to check if the name is already exist or not.but now the checking function i make in comment mode so the program can work properly.

    the problem is, when i active the checking function, the program still can work
    BUT
    every time i try to input any name, even though it is already registered or not. IT ALWAYS said registered, therefore, i can not store that name in external file, because it says that it has been registered

    please i need your help
    thank you before

    ow yeah, my algorithm to make this program is, when inputting name, it will read by character(because my teacher forbid us to use string or string.h). therefore i must store it to external file by each character. when checking, i also check it by character, but didn't work

    PS: sorry for my bad English
    -and, i declare boolean.h from my own
    -i cant use string.h, therefore i cant use strcmp, strlen etc(m teacher forbid this in this task)
    -i use windows
    Last edited by Arrozaq Ave; 11-26-2012 at 07:18 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    dot exe files are binary - you shouldn't ever open them in text mode, and I can't see why you want to open it, anyway.

    How about:
    *get the name that the program will be looking for, in the file.
    *keep the name in a file only - no char array is allowed
    *open the file to be checked, and compare each name with the input name, one by one, char by char

    Do you have a filename you want to use for the "target" name (the name we're going to look for)?

    Are you able to write out the target name, to the filename that will hold it yet?

    Work it out step by step, this first, if it's OK.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    do you mean the name the program looking for is write to new file?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Arrozaq Ave View Post
    do you mean the name the program looking for is write to new file?
    Can you use ccarray[] to hold the name to be looked for? Is that OK? You can't enter a name, into the c variable. That is only big enough for ONE char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving a file
    By Isaiah in forum Game Programming
    Replies: 2
    Last Post: 02-09-2012, 03:04 PM
  2. Replies: 7
    Last Post: 01-01-2008, 12:30 PM
  3. Saving to a file!
    By MrMe913 in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 09:54 AM
  4. BMP File Saving
    By X PaYnE X in forum Windows Programming
    Replies: 13
    Last Post: 08-12-2003, 07:43 PM
  5. saving file
    By marco in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 01:36 PM

Tags for this Thread