int main(int argc, char *argv[]){
	
	char inputFileName[100], outputFileName[100], c;
	FILE *inputFile, *outputFile = stdout;//Output is stdout by default
	int i, numStudents = 1, errorFlag = 0;
	
	if(argc == 1)
	{
	
		//Try to open a file from stdin.
		//If it doesn't work, keep trying or q to quit.
		do{
		
			printf("Enter a file to read information from (or q to exit):");
			scanf("%s", inputFileName);
		
			if(strcmp(inputFileName, "q") == 0){
			
				printf("Terminating program...");
				return 0;
			
			}
			else if((inputFile = fopen(inputFileName, "r")) == NULL)
				printf("Error: file not found\n");
	
		}while(inputFile == NULL);
	
	}
	else if(argc == 2)
	{
		
		//Only the input file was given
		strcpy(inputFileName, argv[1]);
		if((inputFile = fopen(inputFileName, "r")) == NULL)
		{
			
			printf("ERROR: Invalid file name");
			return 1;
			
		}
		
	}
	else if(argc == 3)
	{
		
		//Both input and output files were given
		strcpy(inputFileName, argv[1]);
		if((inputFile = fopen(inputFileName, "r")) == NULL)
		{
			
			printf("ERROR: Invalid input file name");
			return 1;
			
		}
		
		strcpy(outputFileName, argv[2]);
		if((outputFile = fopen(outputFileName, "w")) == NULL)
		{
			
			printf("ERROR: Invalid output file name");
			return 1;
			
		}
		
	}
	
	//Find how many students are here
	numStudents = getNumStudents(inputFile);
	
	//Create the array of students
	Student students[numStudents];
	
	//Code to scan info in
	for(i = 0; i < numStudents; i++)
	{
		
		//1301,107515018,"Boatswain","Michael R.",CSE, 230,="R01"
		
		if(fscanf(inputFile, "%d,", &(students[i].term)) != 1)
			errorFlag = 1;
			
		if(fscanf(inputFile, "%d,", &(students[i].id)) != 1)
			errorFlag = 1;
		
		if(fscanf(inputFile, "\"%[^,\"]\",", students[i].lastName) != 1)
			errorFlag = 1;
		
		if(fscanf(inputFile, "\"%[^,\"]\",", students[i].firstName) != 1)
			errorFlag = 1;
		
		if(fscanf(inputFile, "%[^,],", students[i].subject) != 1)
			errorFlag = 1;
		
		if(fscanf(inputFile, "%d", &(students[i].catalogNumber)) != 1)
			errorFlag = 1;
		
		fscanf(inputFile, "%*[^=]=");
		
		if(fscanf(inputFile, "\"%[^\"]", students[i].section) != 1)
			errorFlag = 1;
		
		fscanf(inputFile, "%*[^\n]");//Reads out to the end of the line to avoid errors
		
		if(errorFlag)
			printf("WARNING: Unexpected or invalid input encountered on line %d!\nUnexpected results may occur...\n", i+1);
			
		errorFlag = 0;
		
	}
	
	//We don't need this anymore
	fclose(inputFile);
	
	//Print the output header
	printStudentHeader(outputFile);
	
	//Sort the students
	sortStudents(students, numStudents);
	
	//Print the students
	printStudents(students, numStudents, outputFile);
	
	return 0;
	
}
Esempio n. 2
0
void Global::generatePDFs(QString dirname)
{
  size_t pageCount = 0;

  for (size_t s = 0; s < getNumStudents(); s++)
  {
    Student& student = db()->getStudent(s);
    // Use the student name to form the file name for the repot
    QString clean = student.getStudentName();
    // Convert all non alpha/num chars into an underscore
    for (QString::iterator i = clean.begin(); i != clean.end(); i++)
    {
      if (!i->isLetterOrNumber())
        *i = '_';
    }
    if (clean.length() == 0)
    {
      GINFODIALOG(QString("Cannot render PDF because student %1 does not have a name assigned").arg(s+1));
      return;
    }
    QString pdfname (dirname + "/report-" + clean + ".pdf");
    GDEBUG ("Generating PDF [%s] for student [%s]", qPrintable(pdfname), qPrintable(student.getStudentId()));

    QPrinter printer (QPrinter::HighResolution);
    printer.setOutputFormat (QPrinter::PdfFormat);
    printer.setOutputFileName (pdfname);
    printer.setPageSize(QPrinter::Letter);
    printer.setResolution(150); // DPI for the printing
    printer.setColorMode(QPrinter::GrayScale);

    QPainter painter;
    if (!painter.begin(&printer)) // Check for errors here
      GFATAL("Failed to do QPainter begin()");

    // Can use this code to change the text color, but causes larger PDF files
    // since it must use a color output format instead.
    //QPen penColor(QColor("#000090")); // Change text to dark blue
    //painter.setPen(penColor);

    for (size_t p = 0; p < getNumPagesPerStudent(); p++)
    {
      pageCount++;
      // Add spaces at the end so the widget can resize into the reserved space without a re-layout
      Global::getStatusLabel()->setText(QString("Generating PDF for student %1 of %2, page %3 of %4 (%5 percent)     ").
                                        arg(s+1).arg(getNumStudents()).arg(p+1).arg(getNumPagesPerStudent()).arg(rint(0.5+100.0*pageCount/(1.0*getNumPages()))));
      // Flush out Qt events so that the UI update occurs inside this handler
      Global::getQApplication()->processEvents();

      GDEBUG ("Printing page %zu of %zu for report [%s]", p+1, getNumPagesPerStudent(), qPrintable(pdfname));
      QPixmap pix = getPages()->getQPixmap(p+s*getNumPagesPerStudent());
      // Scale the pixmap to fit the printer
      pix = pix.scaled(printer.pageRect().width(), printer.pageRect().height(), Qt::KeepAspectRatio);
      // Draw the pixmap to the printer
      painter.drawPixmap (0, 0, pix);

      // Print out the student details at the top of the page
      QString title = QString("Name: %1  ID: %2  Page: %3 of %4  Final Grade: %5 of %6").arg(student.getStudentName()).arg(student.getStudentId()).arg(p+1).arg(getNumPagesPerStudent()).arg(student.getTotal()).arg(db()->getTotalMaximum());
      painter.drawText(0, 0, title);

      // Build up a results string to print onto the page
      QString grades ("Results:");
      size_t pageTotal = 0;
      size_t pageMax = 0;
      for (size_t q = 0; q < getNumQuestions(); q++)
      {
        // See if the question is on this page
        GASSERT(Global::db()->getQuestionPage(q) != 0, "Cannot have page 0 assigned for question %zu", q);
        if (Global::db()->getQuestionPage(q) < 0)
        {
          GINFODIALOG(QString("Cannot render PDF because question %1 does not have a page assigned").arg(q+1));
          return;
        }
        if (Global::db()->getQuestionPage(q) == ((int)p+1))
        {
          if (student.getGrade(q) < 0)
          {
            GINFODIALOG(QString("Cannot render PDF for student [%1] because question %2 has no grade assigned").arg(student.getStudentName()).arg(q+1));
            return;
          }
          pageTotal += student.getGrade(q);
          pageMax += Global::db()->getQuestionMaximum(q);
          grades += QString("  Q%1 = %2/%3").arg(q+1).arg(student.getGrade(q)).arg(Global::db()->getQuestionMaximum(q));
          if (student.getFeedback(q) != "")
            grades += QString(" [%1]").arg(student.getFeedback(q));
        }
      }
      grades += QString("  Totals = %1/%2").arg(pageTotal).arg(pageMax);
      if (pageMax == 0)
          grades = QString("No Results For This Page");
      // Wrap the text to fit a bounding box that is the width of the page, align to the bottom of the page
      painter.drawText(0, 30, printer.pageRect().width(), printer.pageRect().height()-30, Qt::TextWordWrap | Qt::AlignBottom, grades);

      // Insert a new page except on the last one
      if (p < getNumPagesPerStudent()-1)
        if (!printer.newPage()) // Check for errors here
          GFATAL("Failed to do newPage() call");
    }

    painter.end();
  }
  Global::getStatusLabel()->setText("");
}