2

Please help! Whenever it outputs the array, it prints garbage :( The purpose of my code is for it to go through a long text file, which has a conversion, like this.

2016-20-5: Bob: "Whats up!"
2016-20-5: Jerome: "Nothing bro!" 

and for it to take this and break it up to like a format like this:

Person's Name: Bob Message Sent: Whats up! Date: 2016-20-5

(BTW there is a file called "char.txt" and if I use a string it works, but I cant use string because some funcs only accept char*) Here is what I have so far, still trying to make it print out this:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    ifstream readchat;
    readchat.open("chat.txt");
    const int MAX = sizeof(readchat);
    char line[MAX];
    char *colon;
    colon = strtok(line, ":");
    while (!readchat.eof())
    {   
        while (colon != NULL)
        {
            cout << line;
            colon = strtok(NULL, ":");
        }
    }
    system("pause");
    return 0;
}
Vertexwahn
  • 6,759
  • 6
  • 50
  • 78
  • 5
    `!readchat.eof()` is not a good loop condition. [c - Why is “while ( !feof (file) )” always wrong? - Stack Overflow](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – MikeCAT Jun 02 '16 at 14:53
  • 5
    *I am only 12 years old so the code might be full of errors and memory leaks* Sorry but you have to be 13 years of age or older to use this site – NathanOliver Jun 02 '16 at 14:53
  • Agreed with @NathanOliver. Unfortunately, your account is _very_ likely to be deleted soon. – ForceBru Jun 02 '16 at 14:55
  • Instead of `char line[MAX];` you should use `std::string line;` in c++. – πάντα ῥεῖ Jun 02 '16 at 14:55
  • This is not doing what you expect: `sizeof(readchat);` This gives the size of the object `readchat` which may be just 18 bytes (any buffers will be held as pointers to separately allocated memory). – Martin York Jun 02 '16 at 14:55
  • 2
    Yes, there are too many errors... `sizeof(readchat)` seems meaningless, variable-length array is not supported in standard C++, `line` is used while uninitialized. – MikeCAT Jun 02 '16 at 14:55
  • @MikeCAT FYI C++ version: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – NathanOliver Jun 02 '16 at 14:56
  • `const int MAX = sizeof(readchat);` -- This does not return to you the file size (if that is what you were intending). – PaulMcKenzie Jun 02 '16 at 14:56
  • @NathanOliver turn 13 in October :( –  Jun 02 '16 at 14:58
  • 1
    First convert your code to using `std::string` rather than arrays (especially line). Second add call to `std::getline(readchat, line, ':')` to read the file. This should make your code work. Third (and most important) go to http://codereview.stackexchange.com to get a review (note: They only review orking code). – Martin York Jun 02 '16 at 14:59
  • 1
    Note: your main issue is that you don't actually read the file. – Martin York Jun 02 '16 at 15:00
  • @πάνταῥεῖ I tried that but some funcs cant take a string as a argument –  Jun 02 '16 at 15:01
  • @elementsoup You can always use `std::string::c_str()` to get a `const char*`. Also don't use `strtok()` there are better methods available to split a string. – πάντα ῥεῖ Jun 02 '16 at 15:02
  • @LokiAstari strtok gives errors complaining I am not using a char –  Jun 02 '16 at 15:04
  • @elementsoup: You **can** use this site at 12 and ask questions. **BUT** you can not have your own account at 12. Because of privacy laws in the USA `Children's Online Privacy Protection Act of 1998 ` if you are under 13 the site is not allowed to ask for your e-mail address. So as long as your parents created the account with their information you can ask questions. – Martin York Jun 02 '16 at 15:06
  • @LokiAstari My parents made my gmail account which I am using. –  Jun 02 '16 at 15:08
  • @πάνταῥεῖ Please tell me which ways there is to split up strings, I hate C funcs and strings. –  Jun 02 '16 at 15:09
  • @elementsoup: That is not enough. This account has to belong to your parental unit. I am not the maker of rules I just read them. Look at [How do I use Stack Exchange if I'm under 13 years old?](http://meta.stackexchange.com/questions/61770/how-do-i-use-stack-exchange-if-im-under-13-years-old) – Martin York Jun 02 '16 at 15:12
  • @elementsoup: Stop using `strtok()`. Use `std::getline(readchat, line, ':')` instead. It will read upto the `:` from the file. – Martin York Jun 02 '16 at 15:14
  • @LokiAstari So should I change the email to theirs? –  Jun 02 '16 at 15:14
  • @LokiAstari I used getline, it printed it out perfectly :) Now all I need is that whenever it encounters a : I want it to break to a newline –  Jun 02 '16 at 15:16
  • @elementsoup: I would go talk to your parents. I can not advise you what to do. – Martin York Jun 02 '16 at 15:16
  • @elementsoup: Add a new line to the output. `cout << line << '\n';` – Martin York Jun 02 '16 at 15:17
  • @LokiAstari Oops, havent though of that, silly me. I will talk with my parents and if it is necessary, I will make them send a email to Stack Overflow. –  Jun 02 '16 at 15:19
  • 1
    Honestly, should just help him with the question instead of rambling on about his age. In the future OP, don't post your age in a question, just post you're only a novice etc etc. – Hatted Rooster Jun 02 '16 at 15:19
  • @GillBates Alright, I will. Although I think it was right of them to tell me that I am underage but it should have strictly been stuck to the topic of this thread. –  Jun 02 '16 at 15:21
  • Now get your code properly reviewed at; http://codereview.stackexchange.com – Martin York Jun 02 '16 at 15:23
  • @LokiAstari Will do, I wanted it to store and print out the date, message and the name of the person. If anyone could tell me how I can achieve that, it will be perfect. –  Jun 02 '16 at 15:26
  • `BTW there is a file called "char.txt" and if I use a string it works, but I cant use string because some funcs only accept char*` Just use `std::string` and then use `c_str()`. If you need a modifyable char pointer then just copy the string contents to a char array. – Hatted Rooster Jun 02 '16 at 15:32
  • @elementsoup: Have a look at: [Reading/writing files to/from a struct/class](http://stackoverflow.com/q/17892448/14065) Its a bout reading objects from a file. Then you can create a vector of these object to store and processes. – Martin York Jun 02 '16 at 15:32
  • @MikeCAT So what would you recommend instead of EOF so that it will read on until it reaches the end of the file? –  Jun 02 '16 at 15:33
  • Try to read and check if the reading was successful. If it was not successful, stop reading. – MikeCAT Jun 02 '16 at 15:35
  • @LokiAstari I am looking through it and I am a bit confused because I think the code is a bit to advanced to what I know and can do since I am only a beginner. –  Jun 02 '16 at 15:46
  • @elementsoup _"Please tell me which ways there is to split up strings"_ See [this post](http://stackoverflow.com/questions/236129/split-a-string-in-c) please. The easiest is `std::getline()` with an `std::isitringstream` IMHO. – πάντα ῥεῖ Jun 02 '16 at 15:47
  • @elementsoup: see https://gist.github.com/Loki-Astari/f72c988fa551a11d268b29d015598ba1 – Martin York Jun 02 '16 at 15:57
  • @LokiAstari Alright, I am starting to get it. Thanks for the brand new code and it would be nice if you could tell me what those bitwise operators are doing there? I am not experienced with them and it would be nice if you could give me a resource that I can read to understand the << and >> and operand thing :) Thanks –  Jun 02 '16 at 16:01
  • @elementsoup: When the object on the left of `<>` is a stream; then these are known as the input/output operators. These operators are already defined for all basic types and `std::string`. But most people will also define them for their own classes as it makes reading/writing an object much simpler to read. – Martin York Jun 02 '16 at 16:05
  • @LokiAstari Thank you sir! Although, your code cannot be ran by my Visual Studios Compiler for some reason. Error: expected a ';' line: 17 –  Jun 02 '16 at 16:12
  • 1
    @elementsoup: Add the proper includes at the top of the file. Try now. – Martin York Jun 02 '16 at 16:17
  • @LokiAstari It works now! :) Can you please explain what you are doing in the code? You are creating a function but never using it and somehow it manages to run? I really am bad at C++ :P –  Jun 02 '16 at 16:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113659/discussion-between-loki-astari-and-elementsoup). – Martin York Jun 02 '16 at 16:48

2 Answers2

2
  1. You can convert a String to a char array / pointer via str.c_str() http://www.cplusplus.com/reference/string/string/c_str/ You can combine this to:

    std::string linestr;
    std::getline ( readchat,linestr);
    char * line = linestr.c_str()`
    
  2. Alternative: read direct to array with readchat.read() http://www.cplusplus.com/reference/istream/istream/read/

Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
wiomoc
  • 869
  • 8
  • 17
1

Answer, thanks to Loki Astari! New code:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream readchat("chat.txt");
    std::string line;
    while (std::getline(readchat, line, ':'))
    {
        std::cout << line << std::endl;
    }
}

Explanation: Used a string instead of char because it is way more neat and is overall way better. TO read the file into my string I used std::getline(readchat, line, ':') which also took care of cutting the string in the :. Then since readchat was read into line, I printed line out and added a endl to make a new line everytime the string was cut.

Martin York
  • 234,851
  • 74
  • 306
  • 532