473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strings and reading from files

79
Hello,

I need to write a small console application that reads in individual lines of text from a specified file. Every line of text looks like this:

<CODE> = <COMMAND>

I want to be able to read in that line, split it in two at the equals sign (for all intents and purposes, the equals sign is just there to separate the text, kind of like a semicolon), and use the CSMapStringToString to store every command, with the code as the key.

What are ways to easily implement this?

Edit: my supervisor suggested that I use the CString class to get the data from the file...unfortunately he went on vacation and he won't be back to help me for another 3 weeks.

Also note that this isn't an assignment, it's for work. I jsut saw the student assignment thread and I thought I'll clarify this.
May 18 '07 #1
5 2283
AdrianH
1,251 Expert 1GB
Use scanf("%<buffersize>[^=]= %<buffersize>[^\n]%*[\n\r]", left, right);
where <buffersize> is the size of the respective buffer.

The first will have an extra space at the end (you put a space between the equal sign and the left and right sides) that you may have to get rid of. If spaces are illegal on the left side add a space and other whitespace characters to the first character class. For more info, read up on scanf format strings.


Adrian
May 18 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Try this:
Expand|Select|Wrap|Line Numbers
  1. ifstream input("record.txt");
  2. string code;
  3. string command;
  4. char ch;
  5.    //do this for each record of the file.
  6.    while ((ch = input.get()) != '=')
  7.    {
  8.       code += ch;
  9.    }
  10.    while ((ch = input.get()) != '\n')
  11.    {
  12.       command += ch;
  13.    }
  14.  
If can use CString you are in C++. scanf() is for C.
May 19 '07 #3
AdrianH
1,251 Expert 1GB
Try this:
Expand|Select|Wrap|Line Numbers
  1. ifstream input("record.txt");
  2. string code;
  3. string command;
  4. char ch;
  5.    //do this for each record of the file.
  6.    while ((ch = input.get()) != '=')
  7.    {
  8.       code += ch;
  9.    }
  10.    while ((ch = input.get()) != '\n')
  11.    {
  12.       command += ch;
  13.    }
  14.  
If can use CString you are in C++. scanf() is for C.
Oh come on wfc, CString is for if you want to limit yourself to a MFC framework. string is for general C++. You should know better. ;)

While I agree with your code (well under UNIX it would be fine but if you ran this under Windows or a Mac, you would either get a '\r' at the end of you command string or at the beginning of your code string), it is inefficient (not a problem if speed is not an issue and the file you have to parse is relatively small and not parsed often). The reason is that you are constantly reallocating memory each time you use the '+=' operator. This can also lead to memory fragmentation.

I've written a parsing document that can be looked at here. It is written for C (C++ has access to these functions as well). I will try and write one for C++ as well in the future.


Adrian
May 20 '07 #4
ahammad
79
Adrian, your way seems to work, but I guess something is wrong with my syntax. I'm still trying to figure it out.

I also tried the other way using CString, but once I type the filename, it seems to just stop running forever because I don't get a "done" message that I put in there.

Edit: I can't seem to figure out what I did wrong so here is the relevant output and code:

Adrian, I tried your method and this is what I end up with:

Expand|Select|Wrap|Line Numbers
  1. Please enter the filename: test.cpf
  2. ╠ = ╠
  3. ╠ = ╠
  4. ╠ = ╠
  5. ╠ = ╠
  6. ╠ = ╠
  7. ╠ = ╠
  8. ╠ = ╠
  9. ╠ = ╠
  10. ╠ = ╠
  11. ╠ = ╠
  12. ╠ = ╠
  13. ╠ = ╠
  14. ╠ = ╠
  15. ╠ = ╠
  16. ╠ = ╠
  17. ╠ = ╠
  18. ╠ = ╠
  19. done
  20. Press any key to continue . . .
Here are the relevant parts of the code that I used to do this:

Expand|Select|Wrap|Line Numbers
  1. char filename[100], code[999], command[999];
  2. FILE *pFile;
  3.  
  4. pFile = fopen(filename, "r");
  5.  
  6.     for (int k = 0; k < 17; k++){
  7.  
  8.  
  9.         fscanf(pFile, "%999[^=]= %999[^\n]%*[\n\r]", code, command);
  10.  
  11.         cout << code[k] << " = " << command[k] << endl;
  12. }
What am I doing wrong here? Something tells me that I am not really understanding the <buffersize> that I should put in there.

Thanks
May 22 '07 #5
AdrianH
1,251 Expert 1GB
Adrian, I tried your method and this is what I end up with:

Expand|Select|Wrap|Line Numbers
  1. Please enter the filename: test.cpf
  2. ╠ = ╠
  3. ╠ = ╠
  4. ╠ = ╠
  5. ╠ = ╠
  6. ╠ = ╠
  7. ╠ = ╠
  8. ╠ = ╠
  9. ╠ = ╠
  10. ╠ = ╠
  11. ╠ = ╠
  12. ╠ = ╠
  13. ╠ = ╠
  14. ╠ = ╠
  15. ╠ = ╠
  16. ╠ = ╠
  17. ╠ = ╠
  18. ╠ = ╠
  19. done
  20. Press any key to continue . . .
Hmmm, well that doesn't look right. ;)
Here are the relevant parts of the code that I used to do this:

Expand|Select|Wrap|Line Numbers
  1. char filename[100], code[999], command[999];
  2. FILE *pFile;
  3.  
  4. pFile = fopen(filename, "r");
  5.  
  6.     for (int k = 0; k < 17; k++){
  7.  
  8.  
  9.         fscanf(pFile, "%999[^=]= %999[^\n]%*[\n\r]", code, command);
  10.  
  11.         cout << code[k] << " = " << command[k] << endl;
  12. }
What am I doing wrong here? Something tells me that I am not really understanding the <buffersize> that I should put in there.

Thanks
Close make declaration code[1000], command[1000];, set the last element to '\0' in each and don't use indicies on code and command when outputing unless you ment to make code and command a 2d array. What you are telling it to do is output a character at index k, not the entire character string.


Adrian
May 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: cjl | last post by:
Hey all: I am working on a little script that needs to pull the strings out of a binary file, and then manipulate them with python. The command line utility "strings" (part of binutils) has...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
5
by: Jamie | last post by:
I have a file that was written using Java and the file has unicode strings. What is the best way to deal with these in C? The file definition reads: Data Field Description CHAR File...
5
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. ...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
12
by: romayankin | last post by:
I plan to write site on a few language. For this reason I'm trying to find the most convenient way to output strings depending on their unique ID. As far as I plan to have pretty much strings...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
2
by: Lee J | last post by:
Gang, I am self taught in C and fairly proficient for last 20 years. Don't program much, just little utilities for work. Been wanting to create windows forms and found MS VC++ Express. ...
6
by: Sabiyur | last post by:
Hi All, The application I am doing requires hundreds of strings to be stored & retrieve those randomly. I think I can use microsoft resource ".rc" file to store the strings. I am not sure, how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.