![]() |
|
#1
|
|||
|
|||
Need guidance with strchrI'm currently working on a program that requires me to read strings from a file "teams.txt"
Code:
I need to break up the strings using strchr, strcpy, atoi, atof, but my instructor expects us to use the book to figure this out; the problem is that our book doesn't cover these subjects, and my professor is currently away on a family emergency. I don't know where to start, so a push in the right direction would be greatly appreciated. A couple of questions: - Can I use cin.getline() to read from the file, or is there a different method to get the string? - Regardless of which method I use to get the string from the file, can I store the retrieved information into an array? For example: CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Need guidance with strchrMaybe strtok() will do the job for you:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Need guidance with strchractually, i don't think strtok is food for this...(or maybe i don't know how to use it)
anyway, here's what i think the process operation should be like: (note, not exactly sure what you wanted to do with the output, so I showed it on screen, and pasted in a txt file, according to what you need to do, you can modify the string further using the same method). CPP / C++ / C Code:
my output: Code:
|
|
#4
|
|||
|
|||
Re: Need guidance with strchrThanks a lot!
Just seeing how strchr/strcpy are used helped 100%, I'm going to try to figure out how to use strchr to find the ":" and change it to "\0", and from there I"ll use strcpy to get each string set into the right structure. Thanks again. |
|
#5
|
||||
|
||||
Re: Need guidance with strchrQuote:
I'm not sure where the "CPP" part of your "jnCPP" name comes into play, but whatever it is that you're doing is most definitely NOT C++ code. It is a mixture of C and C++ that is, at best, a bastardization of both languages. You may want to describe the data format in more detail. I don't know what the integer number sequences are related to in your data. They kind of look like the combined scores of three games for "both teams" in a basketball game or something. In other words, how do we separate them from the text file into something more meaningful if we don't know what they mean? Decide if you're using C or C++. Inter-mixing the two at this level is probably not a good idea. Here is a C-centric example using strtok: CPP / C++ / C Code:
Output: Code:
Noting, of course, that there is an obvious "bug" in this code whereby if the entire file is not within BUFSIZ bytes, then the parse function is unlikely to work properly. Just a little side angle for those who would submit it without checking. MxB |
|
#6
|
|||
|
|||
Re: Need guidance with strchrQuote:
I don't recommend using /0 as it is a NULL character, and a NULL character signifies the end of the char array. try using ' '(space) instead. Quote:
MxB, I think he's in the same bucket as me, out school teacher is extremly outdated with current C++ (heck, my teacher doesn't know what namespace std is >.<"), and we might have pick some hybrid coding habits, if you see such habits, now or in the future, can you please point them out to the CPP version? thanks. |
|
#7
|
|||
|
|||
Re: Need guidance with strchrQuote:
C strings are traditionally terminated by one byte with the value of zero affectionately known as the "null" character and for which the constant '\0' is defined for use in your code. You'll sometimes see descripters named to indicate this like MBob's "p_szdata" up there. That's (pointer to string zero-terminated)data. The sz is more of a windows style thing. NULL is an int sized void pointer with the value of zero. I get a warning if I try to use NULL as you have above. CPP / C++ / C Code:
Code:
Quote:
Yes , it messes it up. Do some experiments and see for yourselves. As far as your instructor learning you old stuff, there's nothing wrong with knowing the basic building blocks. Last edited by Howard_L : 21-Nov-2009 at 10:41.
|
|
#8
|
|||
|
|||
Re: Need guidance with strchrwhat are
CPP / C++ / C Code:
also look at how null is defined, in C++ it looks like 0 to me :-S CPP / C++ / C Code:
(footnote: this is getting offtopic, if you want to continue, I can PM you my MSN or Y!M adress, and we can do it there) PS: just compile this code and run it, tell me what you get CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: Need guidance with strchrYes, the value is zero for both. I said that.
The thing is that '\0' (null) is a char and "NULL" is a pointer. That's why I get the warning. Read what your textbook says about each (or don't and keep using NULL as you are , sorry) (is this my son?) |
|
#10
|
|||
|
|||
Re: Need guidance with strchrQuote:
In the C standard: "The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant." and "The macros are NULL which expands to an implementation-defined null pointer constant...." (other macros are explained following this.) In the C++ standard: "The macro NULL is an implementation-defined C++ null pointer constant..." However, there is an interesting footnote: "Possible definitions include 0 and 0L, but not (void*)0" Now what the heck do they mean by that funky footnote??? Well, look it up: "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion." Try the following: CPP / C++ / C Code:
1. Save it as testc.c and compile it with your favorite C compiler. 2. Make a copy named testcpp.cpp and compile it with your favorite C++ compiler. Output from testc.c from GNU gcc 4.1.2 on a 32-bit Linux system: Code:
Output from testcpp.cpp compiled with GNU g++ 4.1.2 on a 32-bit Linux sytem: Code:
Regards, Dave |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python novice in need of guidance | adam_dynamic | Python Forum | 7 | 21-Feb-2009 11:17 |
| Complete novice in search or guidance... | adam_dynamic | Miscellaneous Programming Forum | 3 | 15-Jan-2009 08:22 |
| Some Guidance Needed | Logistical Solu | Miscellaneous Programming Forum | 1 | 24-Apr-2007 08:06 |
| need guidance : on creating login and shopping cart | batrsau | Web Design Forum | 3 | 27-Mar-2006 05:53 |
| Guidance on Web Marketing | 4-motorcycles | Search Engine Optimization Forum | 12 | 29-Apr-2004 19:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The