there're input1.txt command1.txt given files
subject asking for create a result1.txt file with the data from the given files.

there's no any bug while compile,
but when running the program windows system pop out error report that:

***.exe has encountered a problem and needs to close.
We are sorry for the inconvenience.

i would like to know which problem of this program meet and which part should be debug.

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

using namespace std;

int main()
{
	FILE * finput;
	FILE * fcommand;
	FILE * crfile;

//	ifstream finput;
//	ifstream fcommand;
//	ifstream crfile;

	int max_terms = 10;
	int q = 0;

	char temp;

	typedef struct pac{
	char strinput[32];
	};
	typedef struct com{
	char strcommand[24];
	};

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

	int timeslot1[max_terms];
	int timeslot2[max_terms];

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

//	finput.open("input1.txt");
//	fcommand.open("command1.txt");
//	ofstream crfile("result1.txt");

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

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

		if(packe[q].strinput[0]!='\n') q++;

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

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

	fclose(finput);
	fclose(fcommand);
	fclose(crfile);

//	finput.close();
//	fcommand.close();
//	crfile.close();

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

thank you.

Recommended Answers

All 2 Replies

You are misusing fscanf() to scan the integers, you need to apply the address-of operator &

for(int i=0; i<max_terms;i++)
{
    fscanf(finput,"%d", [B]&[/B]timeslot1[i]);
    fgets(packe[q].strinput, 32, finput);

    if(packe[q].strinput[0]!='\n') q++;

    fscanf(fcommand,"%d", [B]&[/B]timeslot2[i]);

    fgets(comm[i].strcommand, 24, fcommand);

    // Note that you may have incremented q by one, above,
    // whenever that is the case, what does packe[q].strinput 
    // contain at this point?
    cout<<packe[q].strinput<<endl<<comm[i].strcommand<<endl;
}

actually that is uncomplete programme.
the data in input1.txt is :

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

and command1.txt is:

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

the retult1.txt format should be:

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

packe[q].strinput is the line text(string) like C0A8001.......
q is used as count the number of the string recorded.

thanks a lot, it's work.

but the next problem is that data in packe[q].strinput are unknown words.
which command should be corrected?

i'm trying to debug and find the way now.
actually i'm still new in c++ and still need to learn more.

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.