| | |
Input files with multiple columns
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
I need some help, fairly new to C++ and I know a little about C. What i am trying to do is get a txt file that looks like this
4 //first number tells how many rows are coming next.
3 54
2 51
9 32
2 34
what i want to do is get the numbers into a array so that i can use them later on in a function. Here is what i wrote so far, and is there a better way in C++.
4 //first number tells how many rows are coming next.
3 54
2 51
9 32
2 34
what i want to do is get the numbers into a array so that i can use them later on in a function. Here is what i wrote so far, and is there a better way in C++.
C++ Syntax (Toggle Plain Text)
# include <stdio.h> void READFile(char* filename) { int num; FILE* inputfile; if ((inputfile = fopen(filename, "r")) ==NULL) { printf("file not found, unable to open\n"); } fscanf( inputfile, "%d", num); int color[num]; int value[num]; for (int i = 0; i< num; i++); { fscanf(inputfile, "%d","%d", &color, &value); } }
0
#2 Oct 18th, 2009
Here's your code, translated in to C++.
Note that by declaring the color and value arrays in the function, they only exist in that function. Perhaps they should be declared in main( ) and passed to the function?
I don't think your C input was quite correct, you input to the address of the array, but never incremented to fill other array elements. Should that have been
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; void READFile(char* filename) { int num; int color[100]; //make this large enough for any problem set int value[100]; ifstream inputfile; inputfile.open( filename ); if ( !inputfile ) { cout << "file not found, unable to open\n"; return; //added } inputfile >> num ; /* int color[num]; //oops, not yet legal in C++ int value[num]; //although array declaration with variables //is supported in some compilers */ for (int i = 0; i < num; i++); { inputfile >> color[i] >> value[i]; } }
Note that by declaring the color and value arrays in the function, they only exist in that function. Perhaps they should be declared in main( ) and passed to the function?
I don't think your C input was quite correct, you input to the address of the array, but never incremented to fill other array elements. Should that have been
C++ Syntax (Toggle Plain Text)
fscanf(inputfile, "%d","%d", &color[i], &value[i]);
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- ORDER BY Multiple Columns (MySQL)
- Vector with input from txt file with multiple columns/rows (C++)
- [IRowsetFastLoad insertions for multiple columns] (C++)
- Reading 2 diffrent input files and outputting to 2 different output files (C++)
- updating multiple columns in multiple rows (PHP)
- reading multiple files in loop (C++)
- HELP with 2D Arrays Input Files-CrimsonEd/QBasic (Legacy and Other Languages)
- clean up of temp files for multiple users (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: How does one tolower an entire string?
- Next Thread: Help with the number guessing game!!!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






