How can I store string in a text file??Here are my codes

Discussion in 'C' started by askmewhy25, Feb 1, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Can someone help me to convert the if and elseif statements to switch-case format and i want to store my strings to a text file how will i do that?

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    
    main(){
           char string[16][128],temp[128],search[128];
           int i=0,s=0,rep,sel,x,eq;
           do{
              do{
                 system("cls");
                 printf("String Database\n\n");
                 printf("1 - Store a String to the Database\n");
                 printf("2 - Remove a String from the Database\n");
                 printf("3 - View Strings in the Database\n");
                 printf("4 - Search a String in the Database\n");
                 printf("5 - Exit\n");
                 scanf("%d",&sel);
                 }while(sel!=1&&sel!=2&&sel!=3&&sel!=4&&sel!=5);
              
              if(sel==1){
                         do{
                            system("cls");
                            printf("Input a string: ");
                            scanf("%s",&string[s]);
                            s++;
                            do{
                               system("cls");
                               printf("String successfully added\n");
                               printf("Enter another string?\n\n");
                               printf("1 - Yes\n");
                               printf("2 - No\n");
                               scanf("%d",&rep);
                               }while(rep!=1&&rep!=2);
                            }while(rep==1);
                         }
              else if(sel==2){
                   do{
                      system("cls");
                      printf("Select position of string to remove:\n");
                      for(i=0;i<s;i++)
                         printf("%d-%s\n",i+1,string[i]);
                         scanf("%d",&x);
                         strcpy(string[x-1],"");
                         s--;
                         strcpy(temp,string[s]);
                      for(i=x;i<s;i++)
                         strcpy(string[i-1],string[i]);
                         strcpy(string[s-1],temp);
                      do{
                         system("cls");
                         printf("String successfully deleted\n");
                         printf("Remove another string?\n\n");
                         printf("1 - Yes\n");
                         printf("2 - No\n");
                         scanf("%d",&rep);
                         }while(rep!=1&&rep!=2);
                      }while(rep==1);
                   }
              else if(sel==3){
                   system("cls");
                   for(i=0;i<s;i++)
                      printf("%d - %s\n",i+1,string[i]);
                      printf("\nPress any key to return to main menu");
                   getch();
                   }
              else if(sel==4){
                   do{
                      eq=1;
                      strcpy(search,"");
                      system("cls");
                      printf("Enter string to search: ");
                      scanf("%s",&search);
                      for(i=0;i<s;i++){
                         eq=strcmp(string[i],search);
                         if(eq==0)
                         break;
                         }
                      do{
                         system("cls");
                         if(eq==0){
                         printf("String matched\n");
                         printf("Position of searched string in array is %d",i+1);
                         }
                         else{
                         printf("String not found\n");
                         printf("Search another string?\n\n");
                         printf("1 - Yes\n");
                         printf("2 - No\n");
                         scanf("%d",&rep);
                         }
                         }while(rep!=1&&rep!=2);
                      }while(rep==1);
                   }
              }while(sel!=5);
           }
     
    Last edited by a moderator: Feb 2, 2010
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why do you need help converting if/else if to switch/case? It's REALLY easy. Have you even tried, or are you just assuming it's difficult? Or is the problem that you don't know the syntax?

    Anyway, here's an example:

    Code:
    if (a==1) { ..; }
    else if (a==2) { ..; }
    else if (a==3) { ..; }
    else { ..; }
    
    switch (a)
    {
    case 1: { ..; break; }
    case 2: { ..; break; }
    case 3: { ..; break; }
    default: { ..; break; }
    }
    
    For storing stuff to a file have a look at fopen, fclose, fgets and fprintf.
     
  4. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I know how to use switch format but the problem is when I need to call the data stored inside the string, every time I call them they are always null
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I can't see your screen from here. What input did you give, and what output did you get? What output did you expect?
    Have you tried adding any debug code to make sure what you think is happening really is happening? So for example after the first
    Code:
    printf("Input a string: ");
    scanf("%s",&string[s]);
    does string[0] contain what you expect? You might try adding something like:
    Code:
    printf("Stored '%s' in string[s=%d]\n",string[s],s);
    to verify that the correct string is stored in the correct place.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice