עבור לתוכן

C++ עזרה דחופה עם קבצים!!!

Featured Replies

פורסם

יצרתי מערך מבנים הקורא נתונים מתוך קובץ
בהרצה מופיעה שגיאה 'הנתונים לא נקראו מהקובץ'
הנה הקוד
תודה גדולה למסייעים!!!

 

#pragma warning (suppress : 4996)
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
using namespace std;
#include <cstring>
#include <fstream>

int len(struct PERSON p) {
    int leng = strlen(p.first);
    leng+=  strlen(p.last);
    leng += 4;
    return leng;
}

void  fullName(char* p,struct PERSON &person) {
    char  fullName [52];
    strcpy(person.first, fullName);
    strcat(fullName, " ");
    strncat(fullName, person.middle, 1);
    strcat(fullName, " ");
    strcat(fullName, person.last);
    p = fullName;
}

struct PERSON
{
    char first[25];
    char middle[25];
    char last[25];
    char* fullN;
};

int main() {
    struct PERSON* people;
    int num;
    cout<<"enter num of persons"<<endl;
    cin >> num;
    while (num < 0) {
        cout << "ERROR /n enter new num" << endl;
        cin >> num;
    }
    
    people = new PERSON[num];
    ifstream inData;
    inData.open("data.txt");
    if (!inData.is_open()) {
        cerr << "Cannot open input file\n";
        exit(EXIT_FAILURE);
    }

    int i = 0;
    while (!inData.eof()) {
        char str[100];
        inData >> people[i].first;
        inData >> people[i].middle;
        inData >> people[i].last;
        people[i].fullN = new char[len(people[i])];
    
        fullName(people[i].fullN,people[i]);
        i++;
    }
    inData.close();
    return 0;
}


 

 
 
 
פורסם
  1. א. יש כפתור בסרגל הכלם לקוד, תודה.

ב. len() לא מתחשבת באורך middle.

ג. האם הקובץ קיים בתיקייה ממנה אתה מריץ את התוכנית? מה תוכנו?

פורסם
  • מחבר

א. תודה 🙂

ב.  בmiddle לא השתמשתי בlen

ג. הקובץ נמצא בתקייה ותוכנו:

Mendi Wolf Itz
Chaim Nachman Choen
Hana Rivka Golan

לא ידעתי איפה להכניס, ליתר בטחון שמתי בכל תקייה 🙃

פורסם
ציטוט של ח א

ב.  בmiddle לא השתמשתי בlen

כן אבל אתה מעתיק אותו בfullName(), אז צריך להקצות מספיק זיכרון.

 

מהי השגיאה שמופיעה?

פורסם
  • מחבר

אוקי

התוכנית לא זיהתה את מקום הקובץ, העברתי לתקייה אחרת והריץ מצוין.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים