|
From: Hal Vaughan on 6 May 2008 14:30 On Tuesday 06 May 2008, H.S. wrote: > Ron Johnson wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA1 > > > > On 05/06/08 11:42, H.S. wrote: > >> Hello, > >> > >> In a C++ program I am reading a data file for later processing and > >> computations. While reading that data file, I want to keep track > >> of data items (doubles) read. > >> > >> The data file is just a text file with N lines with C doubles in > >> each line (N and C are known a priori). For now, I just read from > >> the file stream in to a 2D array variable by reading each double > >> at a time. > >> > >> Now I am trying to introduce some sanity checking into this > >> reading block. Here is what I am trying to do: > >> 1. Verify how many doubles I have read in each line. Must be C. If > >> they are not C, then the input file is corrupt. > >> 2. Verify that the total number of data items are NxC. This is > >> simple, I just keep a track of how many numbers I have read. > >> > >> So, how do I go about doing (1) above? I was thinking of somehow > >> checking if I have reached the end of line somehow (EOL?) but > >> haven't found a method to do so. All I have found is EOF. > > > > This smells suspiciously like CompSci homework. > > Nope, it isn't. The program implements an algorithm in my research. I > have the program running, but now I am also generating the data file > automatically. The above request is to catch any errors creeping in > to the data file due to the new program I am writing to generate it. How are you generating the data file? Can't you output a specific character before each EOL char so you can search for it? If you're generating your own file, can't you use serialization? (Or am I confusing that with Java, forgot which does that.) Hal -- To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: Mark Allums on 6 May 2008 14:30 H.S. wrote: > Hello, > > In a C++ program I am reading a data file for later processing and > computations. While reading that data file, I want to keep track of data > items (doubles) read. > > The data file is just a text file with N lines with C doubles in each > line (N and C are known a priori). For now, I just read from the file > stream in to a 2D array variable by reading each double at a time. > > Now I am trying to introduce some sanity checking into this reading > block. Here is what I am trying to do: > 1. Verify how many doubles I have read in each line. Must be C. If they > are not C, then the input file is corrupt. > 2. Verify that the total number of data items are NxC. This is simple, I > just keep a track of how many numbers I have read. > > So, how do I go about doing (1) above? I was thinking of somehow > checking if I have reached the end of line somehow (EOL?) but haven't > found a method to do so. All I have found is EOF. > > thanks, > ->HS > > Not directly helpful, but some suggestions: 1. You might want to learn PERL or Python or Ruby, and do it there. 2. If it has to be C++, learn enough PERL to write a filter for the data file, and transform it so that it has one double per line. 3. Debug the data generator /in situ/ with a good debugger, and bypass the need to do the sanity checking. 4. Find a good C++ reference, and use it. There are several. Slightly more helpful: 1. Read one line at a time in as a string, then operate on the string. 2. C++ has the ability to do everything that C does in a low level way, but why? Use the C++ way, or use the C way: #include <cstdio> #include <iostream> .. . . using namespace std; .. . . ios::sync_with stdio(); .. . . int blah = fscanf(somefile,"%f %f %f %f\n", d1,d2,d3,d4); if (blah != correctvalue) { dosomething(); closefiles(); cout << "error in data file\n"; exit(1); } .. . . // etc. (The ios::sync_with_stdio(); line may differ slightly on different C++ implementations. I haven't used it in a while. May be spelled synch_. Too lazy to look it up. The fscanf line may just be wrong. I quit writing C programs years ago. Too old, memory failing.) -- Mark Allums -- To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: H.S. on 6 May 2008 14:30 Michael Marsh wrote: > On Tue, May 6, 2008 at 1:50 PM, H.S. <hs.samix(a)gmail.com> wrote: >> As I mentioned earlier, the issue is how do I count items read in one line, >> or before the next EOL? Counting total items is not a problem. >> >> Perhaps a different way to say this is, how do I detect if I have reached >> an EOL while reading doubles from a file stream. > > Can you read full lines out into, eg, a stringstream, and parse your > doubles out of that? You'd hit an EOF at the end of each line in that > case. I'm not sure how you'd get stream out line-at-a-time, though > there may be a stream operator that sets the appropriate behavior. Yup, that could be done by getting a line till the end of "\n" character and then parsing the line. I was just wondering if there was any other way (was trying to avoid parsing). > Just a random idea off the top of my head. > -- To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: Hal Vaughan on 6 May 2008 14:30 On Tuesday 06 May 2008, Ron Johnson wrote: > On 05/06/08 12:50, H.S. wrote: > > Robert Baron wrote: > >> What is so terrible about counting the items as they come in? > > > > As I mentioned earlier, the issue is how do I count items read in > > one line, or before the next EOL? Counting total items is not a > > problem. > > > > Perhaps a different way to say this is, how do I detect if I have > > reached an EOL while reading doubles from a file stream. > > Is this a binary file or a text file? Text file. He already specified that. Hal -- To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: H.S. on 6 May 2008 14:40
Ron Johnson wrote: > > Is this a binary file or a text file? hmm. Text. I made it clear in the original post. -- To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org |