Writing to external text file with C or C++
Hi everyone,
I’m certainly glad to have found this place and much credit goes out to the developer and care taker of this forum not to mention all those who partake in all the answers! Thank you - all of you! I do have a question though, or several… Any info would be great. My specialty has been CSS, Javascript, HTML, XHTML and only very little C and C++. However, I am pressed for time on this project and I thought the day would never come when I would need to use C or C++ in a users application, so here it goes; Basically, in a nut shell, I am trying to write a C and/or C++ (perhaps this may not be the best language to go with, I’m not sure) script that will output multiple Javascript string arrays in one single text file, without erasing or overwriting any previous text strings within that .txt file even when called upon again. Here is a sample of the type of string(s) that I would like to see that would essentially be the result after being saved to the file; s[0] = "Prodoct Name^Name.doc^<br>Part Num<br>Order Num<br>mm/dd/yy:"; s[1] = "Prodoct Name^Name.pdf^<br>Part Num<br>Order Num<br> mm/dd/yy:"; s[2] = "Prodoct Name^Name.xls^<br>Part Num<br>Order Num<br> mm/dd/yy:"; s[3] = "Prodoct Name^Name.htm^<br>Part Num<br>Order Num<br>mm/dd/yy:"; etc., etc., etc, The way I have thought about writing the program (Which I have had much difficulty between extra characters, extra whitespace, or nothing at all) would ask the user, line by line, as to what data the user wants to enter. The script begins by asking the user; “Start a New String [1]” “Read Database File [2]” “Quit Program [3]” “Please select (1, 2, 3):_” If the user selects “3”, then presses [ENTER], the program quits. When the user enters “2”, then presses [ENTER], the program reads all of the data in the file and displays it like so on the screen; s[0] = "Prodoct Name^Name.doc^<br>Part Num<br>Order Num<br>mm/dd/yy:"; s[1] = "Prodoct Name^Name.pdf^<br>Part Num<br>Order Num<br> mm/dd/yy:"; s[2] = "Prodoct Name^Name.xls^<br>Part Num<br>Order Num<br> mm/dd/yy:"; s[3] = "Prodoct Name^Name.htm^<br>Part Num<br>Order Num<br>mm/dd/yy:"; Or can be displayed this way to avoid taking up too much real estate on the screen; s[0] s[1] s[2] s[3] s[4] Technically, by reading the file, this will show the user the last string that was written, in the event that user needs to type in the “s[X]=” manually. When the user enters “1”, then presses [ENTER], the script inserts or begins the new string with “s[0]=”. (If not, I would settle for inserting the string manually when prompted). However, the external text file would show: s[0] = “ Program returns these prompts; “New string array successfully started! 1. “What is the name of the product?:_” The user enters the name of the product (blabla), presses [enter], then the program continues to write on the same line in the text file. After the “blabla” is written in the text file, the program also writes “^” just after the blabla. In the text file, the result would be like this: s[0] = “blabla^ Program returns the statement; “Product name: blabla successfully entered!” Then returns this prompt; 2. “Name of document and extension? (Name.doc, Name.pdf, etc.):_ “ The user then types in the name of the document and the extension (blabla.doc), then presses [enter] and the program writes the document and extension name to the external text file on the same line, and also writes “^<br>” after the blabla.doc. In the text file, the result would show: s[0] = “blabla^ blabla.doc^<br> Program returns this statement; “The Document name blabla.doc was successfully entered!” Then returns this prompt; 3. “What is the part number?:_” The user types in the part number, presses [enter], and the program also writes “:<br>” after the 123456 number. In the text file, the result is shown: s[0] = “blabla^ blabla.doc^<br>123456<br> Program returns this statement; “Part number “123456” successfully entered!” Then returns this prompt; 4. “What is the Order Number?_” The user now types in the order number (123456), presses [enter] and the program also writes another “<br>” at the end of the order number. In the text file, the result shows: s[0] = “blabla^ blabla.doc^<br>123456<br>123456<br> Program returns this statement; “Order number “123456” successfully entered!” Then returns this prompt; 5. “What is the start date?:” Finally, the user now types in the start date “MM/DD/YY”, presses [enter] and the program also writes :”; at the end of the date. The external text file now shows the final result of the string array; s[0] = “blabla^blabla.doc^<br>123456<br>123456<br>mm/dd/yy:”; Program returns the final statement; “String has been successfully finished!” Returns this prompt; 6. “Would you like to start a new array (Y/N):__”. If the user selects N, this will quit the program, but if the user selects “Y”, this will start and append a new string on the next or new line as the external text file would show; (Even if the new string array cannot be incremented between the brackets, having a command to request the user to insert the number between the brackets would suffice if this makes it easier). s[0] = “blabla^blabla.doc^<br>123456<br>123456<br>mm/dd/yy:”; s[1] = “ In all, I am certain that the way that this script needs to be constructed is with appending text in mind as I don’t want to erase any data when starting a new string variable. I’ve tried using Javascript to perform the above functions, but I saw the ability with C and or C++ and felt that was a better way to go. But admittedly, I am lost. Perhaps C or C++ may not be the way to go? I am uncertain that other functions could be added for “switch and case”. As for now I have scrapped the code because of difficulty and time constraints. Right now, I have no way for the program to remember the last variable string written and how to make it start on the next line, rather than at the end of the last string written. I have been working on this for a few weeks, and have I’ve gotten nowhere, therefore the code that I currently have would do no justice posting here. I’d rather take lessons from the pros out here… The most difficult for me, is that I am a newbie to C or C++ and have only used very little for past applications in web design. If anyone can come through with this, I promise to credit your name in this application. Any ideas, helpful code, suggestions or another direction that any of you think should be taken would be greatly appreciated. |
Quote:
If you really need to solve this problem using C or C++, you should buy and read "C Programming Language (2nd Edition)" by the famous Kernighan and Ritchie. I write a lot of C code for my day job, and I would solve your problem using a simple Perl script. Best of luck! |
|
I do have a question though, or several
Quote:
|
Quote:
|
Quote:
Your application appears to do a lot of string processing. It is easier to get that right in a short amount of time in scripting language such as Perl. However, it appeared from your question that you were asking us to write the solution for you. As I mentioned before, I write software for a living. I will answer questions, and help you debug your code. But if you need me to write code from scratch, you will have to pay cash up front. |
Quote:
Eventually, it would find errors, but in all the wrong places - even without me changing the code. For example, it would pickup errors on "for loops" then later it wouldn't, would pickup "scanf" as error then wouldn't, would write to file then wouldn't, but would not show any errors either, would throw errors as soon as it saw "int",then it wouldn't, then every once in a while it would tell me that one or more libraries where unavailable. It really became more obvious that something was really wrong when I decided to use a "switch "case" and "break" for a very simple code that I knew worked and threw all sorts of errors. This is where my frustration was causing me uncertainty whether if it was the coding or not. That's when I decided to write here looking for other solutions or fresh ideas that I hadn't thought of or perhaps missed in the code that I was using. I wound up downloading LabView which has a compiler and everything now works perfectly - even finding the right errors. Again, I certainly appreciate the offer though, and if I hadn't figured it out, I probably would have hired or asked you... |
gcc is the linux built-in compiler, and has many switches of it's own.
Parameter handling is entirely different from standard. cmds: man gcc and gcc --help will yield the jist of it. From what I understand, you're needing array handling, and with c or c++ you need to understand that it's done mainly by arbitrary confinement. not as in row, column access, but addressing. So, that you can manipulate it more easily. Within RAM, instead of drive access. sample: main : array_name[1,2,4,j++] will give with j incrementing the superincreasing numbers. This will give you an 'adaptable' j_function. So, when gcc is launced, your parameters have to be in line with access, including terminal input. Among all this code reference, take it easy. If it were me, I'd throw a few wait cycles, and let the RAM settle. People mash keyys. sample: funct.time.wait(cls*gets(" ")); is a classic way to prevent someone from key-jamming... |
All times are GMT -5. The time now is 03:38 AM. |