c++ by using argc and argv read the text file -
////////// new update!!!!! //////////
.txt have 15 numbers , last number "15" 1. try count how many(save index) of numbers in .txt file. 2. create dynamic array size index. 3. save number dynamic array.
question: how cover char dynamic array int dynamic array.
i got garbage output in terminal:
open sucessues!! index: 15 buffer: 15 0 1073741824 0 1073741824 2136670223 32767 -1680479188 32767 0 0 0 0 0 0 0 int main(int argc, char* argv[]) { char *nptr = argv[1]; char *buffer = new char[5]; int index = 0; ifstream fin(nptr); //open file if (argc > 1){ // allocate memory if(!fin){ cout << "can't read file!!" << endl; return -1; } if(fin){ cout << "open sucessues!! " << endl; } while (!fin.eof()){ fin >> buffer; index++; //counting here!!! } cout << "index: " << index << endl; //print out counting results! cout << "buffer: " << buffer << endl; // checking last number! should "15" delete[] buffer; // buffer = null; int *number = new int[index]; char *temp = new char[index]; int *home = number; //home while(!fin.eof()){ fin >> temp; *number= atoi(temp); //im confessing right here!!! number++; temp++; } number = home; (int = 0; < index; ++i) { cout << *number << endl; //*number print out garbage, don't know why! number++; } fin.close( ); } return 0; } /////************
//////// old ///////// don't read /////i wondering how use argc , argv read file: numbers.txt(few numbers inside). goal is: read file ./sort in terminal like: ./sort numbers use buffer , index count how many number inside, use index create dynamic array, read file again, change "number" int using atoi.
i got segmentation fault: 11 after type: ./sort numbers in terminal.
can me here? need array sort number. here got far:
int main(int argc, char* argv[]) { char *nptr = argv[1]; char *buffer[3]; int index = 0; ifstream fin(nptr); //open file // allocate memory if(fin.is_open()){ cout << "open" << endl; while(!fin.eof()){ fin >> *buffer; index++; } cout << index << endl; }
char *buffer[3];
creates array of 3 pointers characters. not allocate storage point at. not assign storage pointed at. these pointer pointing @ anything. valid memory, invalid memory, older brother's porn stash, don't know. if lucky, point invalid memory , program crash.
fin >> *buffer;
attempts place string read file memory pointed @ first of 3 pointers above. since don't know point, don't know input file written. odds pretty try write invalid memory , program crash.
to solve this, allocate storage, point pointer @ storage, , read pointer.
eg.
char *buffer[3]; char storage[128]; buffer[0] = storage;
then later
fin >> *buffer;
that said, don't think want @ all. more
char *buffer[3];
should
char buffer[3];
in case
fin >> *buffer;
will read 1 character file buffer, that's typo ,
fin >> buffer;
is intended. warning!!! still crash if string read fin longer 2 characters. want rethink in general.
if allowed use std::string , std::vector, seeing still in semester instructor expects teach code having hit things rocks , maybe rub twigs produce fire.
Comments
Post a Comment