Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By ScotchandTweed

Trouble: Program to move strings from 1 file to another.

This is a discussion on Trouble: Program to move strings from 1 file to another. within the C Programming forums, part of the General Programming Boards category; Hello! My objectives for this program is to open and read a file "dict.txt" which has the following data: 4 ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Trouble: Program to move strings from 1 file to another.

    Hello!

    My objectives for this program is to open and read a file "dict.txt" which has the following data:
    4
    dog
    pepper
    marker
    teapot

    It needs to rotate the inner letters one to the right so you'd end up with "mrkear" etc, and then write those words into a new file "scramble.txt" one per line.

    I'm having a lot of problems understanding the program. Compiling is giving me a lot of warnings/errors.

    I'm new to this and taking an online course so my resources for help are limited. My text is not being too helpful either.

    If anyone is willing to school this novice I'd be eternally grateful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAXW 10     //max number of words in file
    #define MAXS 81        //max size of string
    
    
    void scramble(char *array[][MAXS]);
    
    
    int main(void)
    {
        int h = 1, i, num_words;    
        char words[MAXW][MAXS];
        char temp; //to hold string of number of words
        FILE *fpin, *fpout;
    
    
        if((fpin = fopen("dict.txt", "r")) == NULL) //error check
        {
            printf("Error: Couldn't open file.\n");
            exit(1);
        }
        
        
        temp = fscanf(fpin, "%s", words[0]); //reads number on first line 
        if(num_words = atoi(words[0]) != 0) //converts char to int and validates data
        {
            printf("Invalid line 1. Must but whole number.");
            exit(2);
        }    
        
        
        for(i=0; i<num_words-1; i++) //reads each line/word starting at 2nd line and scrables them
        {
            fscanf(fpin, "%s", words[i]);
            scramble(words[i][MAXS]);
        }
        
            
        if((fpout = fopen("scramble.txt", "w")) == NULL) //error check
        {
            printf("Error: Couldn't open file.\n");
            exit(3);
        }
    
    
        
        for(i=0; i<num_words-1; i++) //puts scrambled words in scramble.txt"
        {
            fputs(words[h], fpout);
            h++;
        }        
    
    
        
        fclose(fpin);
        fclose(fpout);
    
    
        return 0;
    }
    
    
    void scramble(char *array[][MAXS])
    {
        int i = 0, j = 1, length;
        char temp;
            
        if(strlen(*array) < 4) //if word is only 3 letters function will stop
            return;
    
    
        else
        {
            length = strlen(*array-2);//starts with word [i] and moves the letters [j]    
            temp = *array[i][j];        
            for(j = 1; j<length; j++)
            {            
                *array[i][j] = *array[j]+1;
            }
            temp = [strleng(*array-1)];
        }
    }
    Code:
    These are the errors that come up:
    asn.c: In function ‘main’:
    asn.c:35:3: warning: passing argument 1 of ‘scramble’ makes pointer from integer without a cast [enabled by default]
    
    asn.c:8:6: note: expected ‘char * (*)[81]’ but argument is of type ‘char’
    
    asn.c: In function ‘scramble’:
    asn.c:64:2: warning: passing argument 1 of ‘strlen’ from incompatible pointer type [enabled by default]
    /usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char **’
    
    asn.c:69:3: warning: passing argument 1 of ‘strlen’ from incompatible pointer type [enabled by default]
    /usr/include/string.h:399:15: note: expected ‘const char *’ but argument is of type ‘char **’
    
    asn.c:73:17: warning: assignment makes integer from pointer without a cast [enabled by default]
    
    asn.c:75:10: error: expected expression before ‘[’ token

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > fscanf(fpin, "%s", words[i]);
    > scramble(words[i][MAXS]);
    Are you
    a) trying to scramble each word, as you read it.
    b) trying to scramble all the words, after you've read them all.

    For a), use
    void scramble(char word[]);
    and call it with
    scramble(words[i]);


    For b), use
    void scramble(char char words[][MAXS]);
    and call it with
    scramble(words);
    ScotchandTweed likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Thanks for your help. It's slow going but I think I'm starting to make sense of it.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings and file word counting Program
    By hotshotennis in forum C Programming
    Replies: 3
    Last Post: 05-23-2012, 01:28 PM
  2. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  3. Help with a program! (file, pointers,strings)
    By wildarms in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 11:31 AM
  4. trouble with file i/o program.
    By Pyroteh in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2007, 09:29 PM
  5. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21