as the project given input1.txt and command1.txt to create result1.txt

input1.txt:

1   C0A80001 8F22 C0A80002 0050 32D5B9C
2   C0A80001 8B05 C0A80002 0050 12D67B
3   C0A80001 CC08 C0A80002 0050 32D5B9C
4
5
6   C0A80001 C4A3 C0A80002 0050 34F1 

command1.txt

1   output
2   output the queue length 
3   idle
4   output
5   output
6   output the queue length

then result1.txt should and must be:

1   C0A80001 8F22 C0A80002 0050 32D5B9C
2   1
3
4   C0A80001 8B05 C0A80002 0050 12D67B
5   C0A80001 CC08 C0A80002 0050 32D5B9C
6   1

but at last i'd get the actual result1.txt as below:

1 D 
2 y燙 
3 D 
4 

5 

6 L? 

i would like to know that which problem of the below programme meet and how to correct:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <cctype>

using namespace std;
using std::string;

int main(void)
{
    FILE * finput;
    FILE * fcommand;
    FILE * creafile;

    int noofline = 10;
    int q = 0;

    typedef struct pac{
    char strinput[38];
    };
    typedef struct com{
    char strcommand[25];
    };

    struct pac packe[noofline];
    struct com comm[noofline];

    int timeslot1[noofline];
    int timeslot2[noofline];

    finput = fopen("input1.txt","r");
    fcommand = fopen("command1.txt","r");
    creafile = fopen("result1.txt","w");

    for(int i=0; i<noofline; i++){

        fscanf(finput,"%d", &timeslot1[i]);
        fgets(packe[q].strinput, 38, finput);

        fscanf(fcommand,"%d", &timeslot2[i]);
        fgets(comm[i].strcommand, 25, fcommand);

        cout<<timeslot1[i]<<packe[q].strinput<<endl<<comm[i].strcommand<<endl;

        if(packe[q].strinput[0] == ' ') {q++;}

        fprintf(creafile, "%d %s \n", timeslot1[i], packe[q].strinput);
        if(comm[i].strcommand[1] != ' ');
        cout<<comm[i].strcommand[1]<<endl;

        cout<<"queue = "<<q;
        cin.get();
    }

    cout<<"Last queue no. is "<<q<<endl;

    fclose(finput);
    fclose(fcommand);
    fclose(creafile);

    cout<<"Press enter to continue...";
    cin.get();
    return(0);
}

ps: this programme still in modified, the main problem i'd met is

unable to write the correct char packe[q].strinput into result1.txt (that should be C0A80001*****)

it always give me unknown words.
thank you.

Since I don't see that you've posted to the C board and you are using pure C syntax within the code I'd encourage you to post there.

I note a problem with the syntax you're using to declare arrays. When declaring arrays the size needs to equate to a const int, not int, which is what noofline is.

if(packe[q].strinput[0] == ' ') 
{
    q++;
    // Because you have incremented q by one,
    // the fprintf() call below is guaranteed to produce
    // unwanted output ...
}

// You must change your code so that you know whether to use 
// packe[q].strinput or packe[q - 1].strinput ...
fprintf(creafile, "%d %s \n", timeslot1[i],  packe[q].strinput);

thank you, its work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.