Thread: Character Array and For Loop

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Character Array and For Loop

    Hello-

    I am working in C and the code builds without error but the application crashes shortly after starting.

    I will run the same body of code, but the thanks_link[x], folder_path[x] and name[x] variables should change with each loop of the For Loop. i.e. first time through the loop Mary's information is used, second time through John, etc...


    I want to create 8 separate output files for 8 different people using the code below.

    Ex)
    x=1)Mary
    x=2)John
    x=3)Suzy
    x=4)Chris
    x=5)Liz
    x=6)Jim
    x=7)Erica
    x=8)Steve

    I know it has something to do with the character array and the For Loop I am using. Any suggestions on what is going wrong?


    Here is the code:


    Code:
    #include <stdio.h>
    int main (void)
    {
    
     
        int x;
     char filename[20];
    
     
        char *folder_path[8];
     char *thanks_link[8];
     char *name[8];
     char *member_name[8];
    
    
     
     char word1[50];
     char word2[50];
     char word3[50];
     char word4[50];
     char word5[50];
     
     char word6[50];
     char word7[50];
     char word8[50];
     char word9[50];
     char word10[50];
     
     char word11[50];
     char word12[50];
     char word13[50];
     char word14[50];
     char word15[50];
     
     char word16[50];
     char word17[50];
     char word18[50];
     char word19[50];
     char word20[50];
     
     char word21[50];
     char word22[50];
     char word23[50];
     char word24[50];
     char word25[50];
     
     
     
     
     
     FILE *in_file;
        FILE *outFile;
      
       folder_path[1] = "/Mary_369/picks.txt";
       folder_path[2] = "/John_715/picks.txt";
       folder_path[3] = "/Suzy_917/picks.txt";
       folder_path[4] = "/Chris_118/picks.txt";
       folder_path[5] = "/Liz_891/picks.txt";
       folder_path[6] = "/Jim_593/picks.txt";
       folder_path[7] = "/Erica_073/picks.txt";
       folder_path[8] = "/Steve_946/picks.txt";
    
        
       thanks_link[1] = "Thanks_Mary";
       thanks_link[2] = "Thanks_John";
       thanks_link[3] = "Thanks_Suzy";
       thanks_link[4] = "Thanks_Chris";
       thanks_link[5] = "Thanks_Liz";
       thanks_link[6] = "Thanks_Jim";
       thanks_link[7] = "Thanks_Erica";
       thanks_link[8] = "Thanks_Steve";
        
    
       name[1] = "MARY";
       name[2] = "JOHN";
       name[3] = "SUZY";
       name[4] = "CHRIS";
       name[5] = "LIZ";
       name[6] = "JIM";
       name[7] = "ERICA";
       name[8] = "STEVE";
    
    
    
     
     in_file = fopen("current.txt", "r");
    
    
    
         fscanf(in_file, "%s", &word1);                          
      fscanf(in_file, "%s", &word2);
      fscanf(in_file, "%s", &word3);                     
      fscanf(in_file, "%s", &word4);    
      fscanf(in_file, "%s", &word5);
      fscanf(in_file, "%s", &word6);           
       fscanf(in_file, "%s", &word7);    
      fscanf(in_file, "%s", &word8);    
       fscanf(in_file, "%s", &word9);    
       fscanf(in_file, "%s", &word10);
      fscanf(in_file, "%s", &word11);                     
      fscanf(in_file, "%s", &word12);
      fscanf(in_file, "%s", &word13);                        
      fscanf(in_file, "%s", &word14);   
      fscanf(in_file, "%s", &word15);
      fscanf(in_file, "%s", &word16);              
       fscanf(in_file, "%s", &word17); 
      fscanf(in_file, "%s", &word18); 
       fscanf(in_file, "%s", &word19); 
       fscanf(in_file, "%s", &word20);
      fscanf(in_file, "%s", &word21);                     
      fscanf(in_file, "%s", &word22);
      fscanf(in_file, "%s", &word23);                        
      fscanf(in_file, "%s", &word24);   
      fscanf(in_file, "%s", &word25);
    
    
    
        
    for (x = 1; x < 9; x++)
    {
        
        sprintf(filename, "NAME_%d.txt", x);
        outFile = fopen(filename, "w");    
    
    
    
    
    fprintf(outFile, "<METHOD=POST %s ; return true;\">\n\n", thanks_link[x]);
    
    fprintf(outFile, "<INPUT TYPE=\"hidden\" NAME=\"savefile\" VALUE=\"%s\">\n\n", folder_path[x]);
    
    fprintf(outFile, "<INPUT TYPE=\"hidden\" NAME=\"MEMBER'S NAME\" VALUE=\"%s\">\n\n", name[x]);
    
    fprintf(outFile, "<INPUT TYPE=\"hidden\" NAME=\"SUBJECT\" VALUE=\"%s %s %s %s\">\n\n", name[x], word1, word2, word3);
    
    
    }
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    943
    Maybe something like this.
    I'm not sure what you're doing with the "words" that you read from current.txt.
    In your code you are only ever printing out the first three words.
    I changed it to write the first three words for the first person, the next three for the next person, etc.
    It would probably be better for the "ids" to be read from a file instead of being hardcoded.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    #define INFILE      "current.txt"
    #define SAVEFILE    "picks.txt"
    #define NUM_IDS      8
    #define NUM_WORDS   25
    #define MAX_STRING  50
     
    void get_name(const char *id, char *name_only)
    {
        char ch;
        while ((ch = *name_only++ = *id++) != '_' && ch != '\0') ;
        if (ch) *--name_only = '\0';
    }
     
    void to_uppercase(const char *in, char *out)
    {
        while ((*out++ = toupper(*in++)) != '\0') ;
    }
     
    int main()
    {
        const char *ids[NUM_IDS] =
        {
            "Mary_369", "John_715", "Suzy_917",  "Chris_118",
            "Liz_891",  "Jim_593",  "Erica_073", "Steve_946"
        };
     
        FILE *inFile = fopen(INFILE, "r");
        if (!inFile)
        {
            perror(INFILE);
            exit(EXIT_FAILURE);
        }
     
        char word[NUM_WORDS][MAX_STRING];  
        for (int i = 0; i < NUM_WORDS; ++i) 
            fscanf(inFile, "%s", word[i]);
     
        for (int i = 0; i < NUM_IDS; ++i)
        {
            char filename[MAX_STRING + 10];
            sprintf(filename, "%s_%d.txt", ids[i], i + 1);
     
            FILE *outFile = fopen(filename, "w");
            if (!outFile)
            {
                perror(filename);
                exit(EXIT_FAILURE);
            }
     
            char name_only[MAX_STRING], uppercase_name[MAX_STRING];
            get_name(ids[i], name_only);
            to_uppercase(name_only, uppercase_name);
     
            fprintf(outFile,
                "<METHOD=POST Thanks_%s ; return true;\">\n\n",
                name_only);
            fprintf(outFile,
                "<INPUT TYPE=\"hidden\" NAME=\"savefile\" VALUE=\"/%s/%s\">\n\n",
                ids[i], SAVEFILE);
            fprintf(outFile,
                "<INPUT TYPE=\"hidden\" NAME=\"MEMBER'S NAME\" VALUE=\"%s\">\n\n",
                uppercase_name);
            fprintf(outFile,
                "<INPUT TYPE=\"hidden\" NAME=\"SUBJECT\" VALUE=\"%s %s %s %s\">\n\n",
                uppercase_name, word[3*i], word[3*i+1], word[3*i+2]);
     
            fclose(outFile);
         }
     
         return 0;
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thank you john.c for the response and support.
    Let's not focus on the input file and the "words". Is it possible to have the variables in the For Loop iterate up one spot for each separate output file so the results look like the below?

    Ex)

    Output File #1 for Mary
    File Name -- "Mary_1.txt"

    Thanks_Mary
    /Mary_369/picks.txt
    MARY

    Output File #2 for John
    File Name -- "John_2.txt"

    Thanks_John
    /John_715/picks.txt
    John



    Etc...


    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
      
    
     int x;
    
     char filename[20];
     
    
      
    
     char *folder_path[8];
    
     char *thanks_link[8];
     char *name[8];
    
     char *member_name[8];
    
     
    
     
      
    
      FILE *outFile;
    
       
    
       folder_path[1] = "/Mary_369/picks.txt";
    
       folder_path[2] = "/John_715/picks.txt";
       folder_path[3] = "/Suzy_917/picks.txt";
    
       folder_path[4] = "/Chris_118/picks.txt";
       folder_path[5] = "/Liz_891/picks.txt";
    
       folder_path[6] = "/Jim_593/picks.txt";
    
       folder_path[7] = "/Erica_073/picks.txt";
    
       folder_path[8] = "/Steve_946/picks.txt";
     
    
         
    
       thanks_link[1] = "Thanks_Mary";
    
       thanks_link[2] = "Thanks_John";
    
       thanks_link[3] = "Thanks_Suzy";
       thanks_link[4] = "Thanks_Chris";
    
       thanks_link[5] = "Thanks_Liz";
    
       thanks_link[6] = "Thanks_Jim";
    
       thanks_link[7] = "Thanks_Erica";
    
       thanks_link[8] = "Thanks_Steve";
    
         
    
     
    
       name[1] = "MARY";
    
       name[2] = "JOHN";
    
       name[3] = "SUZY";
       name[4] = "CHRIS";
    
       name[5] = "LIZ";
    
       name[6] = "JIM";
    
       name[7] = "ERICA";
    
       name[8] = "STEVE";
    
     
    
         
    
    for (x = 1; x < 9; x++)
    
    {
    
         
    
        sprintf(filename, "NAME_%d.txt", x);
    
        outFile = fopen(filename, "w");    
    
    
    
    fprintf(outFile, "%s\n\n", thanks_link[x]);
    
     
    
    fprintf(outFile, "%s\n\n", folder_path[x]);
    
     
    fprintf(outFile, "%s\n\n", name[x]);
    
     
    
     
    
    }
    
     
    
     
    
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,673
    Arrays start from 0, not 1. You are writing outside the bounds of your allocated memory every time you access arrayname[8].

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thanks rags. The application did not crash this time. Something that "simple" would cause the entire executable to crash?

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    302
    Well, it's not really that simple. You're trying to read/write from memory that isn't yours (allocated by you for you). That's a big enough problem to cause a crash.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2014, 08:52 AM
  2. Nested loop Character box
    By golf_fanatic in forum Tech Board
    Replies: 13
    Last Post: 04-25-2012, 11:05 PM
  3. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  4. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  5. Character in Array to end for loop.
    By mattz in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 11:05 AM

Tags for this Thread