Help! Just a simple program, but I can't get it to work.

This is a discussion on Help! Just a simple program, but I can't get it to work. within the C Programming forums, part of the General Programming Boards category; I have to write a program for school that opens a file, reads in the first 50 words of a ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Question Help! Just a simple program, but I can't get it to work.

    I have to write a program for school that opens a file, reads in the first 50 words of a paragraph into an array, then print the words to an output file. Everything appears to be working, but when I view the output file it is nothing but garbage! Can someone please help me? Here is what i have so far:

    #include <stdio.h>


    void main()
    {
    char infile[81];
    char outfile[81];
    char word[50][20];
    int i,j;
    FILE *fin, *fout;
    printf("\n Enter a input file name.");
    gets(infile);
    fin=fopen(infile,"r");
    printf("\n Enter the name of the out file.");
    gets(outfile);
    fout=fopen(outfile,"w");
    if (fin!=NULL)
    {
    if (fout!=NULL)
    {
    for(i=0;i<50;i++)
    {
    for(j=0;j<20;j++)
    {
    word[i][j]=0;
    }
    }
    for(i=0;i<50;i++)
    {
    for(j=0;j<20;j++)
    {
    fscanf(fin, "%s", &word[i][j]);
    }
    }
    for(i=0;i<50;i++)
    {
    for(j=0;j<20;j++)
    {
    fprintf(fout, "%s", word[i][j]);
    }
    fprintf(fout, "\n");
    }
    }
    else
    {
    printf("\n Error opening the output file!");
    printf("\n The program will end now!");
    }
    }
    else
    {
    printf("\n Error opening the input file!");
    printf("\n The program will end now!");
    }
    }

    Thanks in advance!!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    your inner loop is a problem!

    fscanf(fin, "%s", &word[i][j]);
    this reads a string into a char -> BAD

    try this

    for(i=0;i<50;++i)
    fscanf(fin,"%s",word[i]);

    and btw: you do not need to initialize word with this nested loops!!

    you generally do not need to initialize variables, but if you think you need it, then use memset() or initialize during declaration...
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    The program also says that the words must be limited to 20 characters in length. Sorry I didn't out that in the first post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 10:04 PM

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