2011-04-11 14 views
5

मुझे किसी फ़ंक्शन में फ़ाइल को पास करने के तरीके को समझने में कठिनाई हो रही है।फ़ंक्शन में फ़ाइल कैसे पास करें?

मेरे पास 20 नाम और 20 टेस्ट स्कोर वाली एक फ़ाइल है जिसे किसी फ़ंक्शन द्वारा पढ़ना आवश्यक है। फ़ंक्शन तब नाम और स्कोर को छात्र नामक संरचना में असाइन करेगा।

मेरा प्रश्न यह है कि मैं उपयुक्त पैरामीटर के साथ फ़ंक्शन कॉल कैसे लिखूं। ? मेरे फ़ंक्शन को फ़ाइल में डेटा पढ़ने के लिए। धन्यवाद।

कोड

// ask user for student file 
cout << "Enter the name of the file for the student data to be read for input" << endl; 
cout << " (Note: The file and path cannot contain spaces)" << endl; 
cout << endl; 
cin >> inFileName; 
inFile.open(inFileName); 
cout << endl; 

// FUNCTION CALL how do i set this up properly? 
ReadStudentData(inFile, student, numStudents); 

void ReadStudentData(ifstream& infile, StudentType student[], int& numStudents) 
{ 
    int index = 0; 
    string lastName, firstName; 
    int testScore; 

    while ((index < numStudents) && 
      (infile >> lastName >> firstName >> testScore)) 
    { 
     if (testScore >= 0 && testScore <= 100) 
     { 
      student[index].studentName = lastName + ", " + firstName; 
      student[index].testScore = testScore; 
      index++; 
     } 
    } 

    numStudents = index; 
} 
+2

आपके वर्तमान कोड के साथ आप क्या समस्या का सामना कर रहे हैं? मुझे लगता है कि, आप पैरामीटर को सही तरीके से पारित कर रहे हैं। – iammilind

उत्तर

0

फ़ाइल वस्तु के संदर्भ में ठीक हो रहा है, लेकिन StudentType की सरणी शायद गलत वस्तुओं। इस प्रयास करें:

void ReadStudentData(ifstream& infile, 
std::vector<StudentType>& vecStudents, 
int& numStudents) 
2

जिस तरह से आप समारोह में एक ifstream पारित बिल्कुल ठीक है।

मुझे संदेह है कि समस्या StudentType और उसके आकार (numStudents) की अपनी सरणी को प्रबंधित करने के तरीके में निहित है। मैं कच्चे सरणी के बजाय std::vector का उपयोग करने के लिए अपना कोड बदलने की अनुशंसा करता हूं। आम तौर पर, आपको हमेशा सरणी पर वेक्टर पसंद करना चाहिए जबतक कि आपके पास सरणी का उपयोग करने का वास्तव में कोई अच्छा कारण न हो।

वैक्टर अधिक डेटा समायोजित करने और उनके आकार का ट्रैक रखने के लिए बढ़ सकते हैं, इसलिए आपको यह नहीं करना है।

साथ ही, पैरामीटर सूची के माध्यम से पारित वस्तुओं को संशोधित करने के बजाय वस्तुओं को वापस करने के लिए कार्यों का एक अच्छा विचार है।

#include <vector> 
using namespace std; 

vector<StudentType> ReadStudentData(ifstream& infile) { 
    vector<StudentType> students; 
    string lastName, firstName; 
    int testScore; 
    while (infile >> lastName >> firstName >> testScore) { 
     if (testScore >= 0 && testScore <= 100) { 
      StudentType student; 
      student.studentName = lastName + ", " + firstName; 
      student.testScore = testScore; 
      students.push_back(student); 
     } 
    } 
    return students; 
} 

// call the function 
vector<StudentType> students = ReadStudentData(infile); 

// or if you have a C++11 compiler 
auto students = ReadStudentData(infile); 

// use students.size() to determine how many students were read 
+0

इसमें होमवर्क की गंध है; ओपी वैक्टर का उपयोग करने में सक्षम नहीं हो सकता है और कच्चे सरणी का उपयोग करने के लिए कहा जा रहा था .... और इस बिंदु पर सवाल 2 साल पुराना है। – Casey

संबंधित मुद्दे