tags:

views:

158

answers:

3

This program is intended to read a .txt file to get a set of numbers, and write to another two .txt files called even and odd as follows:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
  int i=0,even,odd;
  int number[i];

  // check to make sure that all the file names are entered
  if (argc != 3) {
     printf("Usage: executable in_file  output_file\n");
      exit(0);
    }

  FILE *dog = fopen(argv[1], "r");
  FILE *feven= fopen(argv[2], "w");
  FILE *fodd= fopen (argv[3], "w");

  // check whether the file has been opened successfully
  if (dog == NULL)
    { printf("File %s cannot open!\n", argv[1]);
      exit(0);
    }

  {  if
    (i%2!=1)
      i++;}

 fprintf(feven, "%d", even);
 fscanf(dog, "%d", &number[i]);
 else {
   i%2==1;
   i++;}
 fprintf(fodd, "%d", odd);
 fscanf(dog, "%d", &number[i]);

fclose(feven);
fclose(fodd);
}
+1  A: 

The { appear after the if() condition. And } should come after fscanf(dog, "%d", &number[i]);

if(i%2!=1){
    i++;
    fprintf(feven, "%d", even);
    fscanf(dog, "%d", &number[i]);
}else {
    i%2==1;
    i++;
}
Bragboy
+1  A: 
{ if (i % 2 != 1)
        i++;
}

fprintf(feven, "%d", even);
fscanf(dog, "%d", &number[i]);
else
{
    i % 2 == 1;
    i++;
}

Should be:

if (i % 2 != 1)
{
    i++;
    fprintf(feven, "%d", even);
    fscanf(dog, "%d", &number[i]);
}
else
{
    i % 2 == 1; //BTW this doesn't do anything.
    i++;
}

EDIT: I've taken a few liberties so this might not be exactly your intent but it should be close enough. You need to work on a lot of small things which hopefully looking at the code will help with. Remember to understand what the functions do. And walk before you run.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
    int number;

    // check to make sure that all the file names are entered

    if (argc != 4)
    {
        printf("Usage: %s <inputfile> <even_outputfile> <odd_outputfile>\n", argv[0]);
        exit(1);
    }

    FILE *dog   = fopen(argv[1], "r");
    FILE *feven = fopen(argv[2], "w");
    FILE *fodd  = fopen(argv[3], "w");

    // check whether the file has been opened successfully

    if (dog == NULL)
    {
        printf("File %s cannot open!\n", argv[1]);
        exit(1);
    }

    if (feven == NULL)
    {
        printf("File %s cannot open!\n", argv[2]);
        exit(1);
    }

    if (fodd == NULL)
    {
        printf("File %s cannot open!\n", argv[3]);
        exit(1);
    }


    while (fscanf(dog, "%d", &number) == 1)
    {
        if (number % 2 == 0)
            fprintf(feven, "%d ", number);
        else
            fprintf(fodd, "%d ", number);
    }

    fprintf(feven, "\n");
    fprintf(fodd, "\n ");

    fclose(dog);
    fclose(feven);
    fclose(fodd);
}
Duck
The part that doesn't do anything was probably intended to be `else if (i % 2 == 1)`
Winder
I assumed the part that doesn't do anything was intended to be a comment.
Gabe
The code needs to write odd numbers to fodd; there should only be one fscanf() near the end of the loop.
Jonathan Leffler
i assumed my program could read an input integers from file called dog and writes the odd numbers to a file fodd, and also write the even integers to a file feven. however, i was able to compile it and no error, but i couldnt excute it, used a.out dog.txt feven.txt fodd, kept giving me core dupmped
i was able to compile my program above; however, i am not able to exute it using a.out dog.txt even.txt odd.txt. if any one can suggest another way of excuting.
@user292489 - I've updated the answer. Take things one step at a time.
Duck
+1  A: 

first

int i=0,even,odd;
int number[i];

means the length of Array number is 0. you should write

if (argc != 3) {
    printf("Usage: executable in_file  output_file\n");
    exit(0);
}

FILE *dog = fopen(argv[1], "r");
FILE *feven= fopen(argv[2], "w");
FILE *fodd= fopen (argv[3], "w");
int num;
while (fscanf(dog, "%d", &num) != EOF)
{
    if(num % 2 == 0)
    {
        fprintf(feven, "%d", num);
    }else
    {
        fprintf(fodd, "%d", num);
    }
}
fclose(feven);
fclose(fodd);
fclose(dog);
neverend
Actually, it should be 'argc != 4' and the usage should mention the even.txt and odd.txt or some such.
Jonathan Leffler