what im trying to do is read in a data from a file into 4 different arrays then also read in the same data using array of structs. the assignment is to show that we know how to use both methods of reading in and sorting the data. i have 3 functions for the arrays (for reading in the arrays, sorting the arrays, then printing) and same for the struct. but when i go to call the array funtions they work then i call the 3 struct functions but they dont work. if i comment out all the array functions and leave just the struct calls it works perfectly. i just cant get it to call both the array functions and the struct functions right after each other.

Dani AI

Generated

This is a classic file-stream state issue. Reading the whole file leaves the stream at end-of-file and sets error flags, so further formatted reads immediately fail. As pointed out, you must reset the stream state and reposition the read pointer if you want to reuse the same ifstream; alternatively, read the data once into memory and use that copy for both array- and struct-based work. The minimal reset pattern is to clear the flags and seek back to the start (see the C++ reference for clear and seekg): basic_ios::clear and basic_istream::seekg.

A recommended, robust approach is to read the file a single time into an in-memory container (for example a std::vector of your record struct), then fill arrays or make copies from that vector. This avoids repeated file I/O and makes debugging easier. Use a read loop that stops on failure (for example while (in >> field1 >> field2)), push each record into the vector, then populate any arrays or perform sorting on copies. See std::vector and std::copy for common idioms.

Troubleshooting checklist: verify the file opened successfully before reading; reset any index counters between passes; avoid using while (!in.eof()); if you mix operator>> and getline handle leftover newlines with ignore; and confirm your read functions do not close the stream themselves. Opening two streams, as did, is a valid quick fix, but resetting a single stream or reading once into memory is cleaner and more efficient.

Recommended Answers

All 3 Replies

When you read something from a file you can not direct it to two different places at the same time. Read the data into the array then either (1) copy the data from the array into the structure, or (2) after the entire file has been read into the array rewind the file back to the beginning and read it again into structures.

ya i tried using rewind but that didnt help. So what i did was make 2 different input file streams and used one to call all the array functions and the other to call all the struct functions then it worked fine.

>>ya i tried using rewind but that didnt help
Then you didn't do it right. If you are using fstreams then you also have to call the clear() method to clear the error (eof) condition flag.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.