1

I am trying to pass a string into a function in C. That string will entered by the user and then passed on to the function to write to a text file. Iknow this seems very basic but I am just learning C.

#include <stdio.h>
#include <string.h>

void read() {

        char text[50][30],buffer[150];

        int i=0;
        FILE *file_in;
        file_in=fopen("test.txt","r");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        while (fgets(buffer,150,file_in)) {
                strcpy(text[i],buffer);
                printf("line %d: %s\n",i,text[i]);
                i++;
        }

        getchar();

        fclose(file_in);
}

void write(char str[])
{
        FILE *file_in;
        file_in=fopen("test.txt","a");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        //write to the file
        fprintf(file_in, "\n%s", str);
//      fputs(str, file_in);

        fclose(file_in);
}

int main()
{
        read();

        char msg[50];

        printf("Enter some text: ");
        puts(msg);

        write(msg); 

        return 0;
}

It writes to the file, but it writes weird characters, not what I actually type. What amI doing wrong?

7
  • What DOES it write? Something like 0x049569...? Commented May 9, 2014 at 23:58
  • I writes something like a box with many characters in it. Commented May 9, 2014 at 23:59
  • Please post what it prints in your question Commented May 10, 2014 at 0:01
  • 1
    You never store a value in msg, so it contains garbage. Commented May 10, 2014 at 0:02
  • I tried to paste the output here but when I paste it it doesn't actually show up on here. When I look at the fileon the vim editor it shows up as ^A. When I open it on gedt it shows up as garbage Commented May 10, 2014 at 0:04

4 Answers 4

2

It looks like you've confused gets with puts. puts writes a string to the console. gets reads a string from the console. Switch them out and your program should work.

Microsoft's compiler often warns against insecure or deprecated functions, like gets. You may use fgets instead, as it doesn't allow buffer overflows.

Here's an example:

fgets(msg, 50, stdin);

or

fgets(msg, sizeof(msg), stdin);
Sign up to request clarification or add additional context in comments.

4 Comments

did you just advise someone to use gets ?
I tired it using gets.It worked but it says it was deprecated. Is there a better way?
@TaylorFlores: Not for text input. fread will read a specified number of bytes, which means it won't stop at the end of a line (when you type Enter). Use fgets.
@flaco I changed my suggestion to fgets
0

First of all: Don't call your functions read() and write() — pick something more specific. The function names read() and write() are already used by the system for low-level file operations, and trying to define them yourself will cause unexpected behavior.

Second: you are never initializing the contents of the msg variable or reading any data into it, so its contents will be random. (Remember that puts() prints data; it doesn't read anything.)

Comments

0

When you write char msg[50];, it contains indeterminate values. It's not zero-initialized or anything.

The puts(msg); line writes this garbage out, and then write(msg); writes that garbage to a file.

I guess you intended to have some code for inputting text, after the printf and before the puts.

NB. In your read() function (which you don't call yet), you should make the fgets buffer size match the width of your array , and you should check you don't run off the end of your array if the file has many lines.

Also it would be wise to name your functions something other than read and write, because in a POSIX environment there are already functions by that name which may clash.

Comments

-1

Here is the solution:

#include <stdio.h>
#include <string.h>

int read() {

        char text[50][30],buffer[150];

        int i=0;
        FILE *file_in;
        file_in=fopen("test.txt","r");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        while (fgets(buffer,150,file_in)) {
                strcpy(text[i],buffer);
                printf("line %d: %s\n",i,text[i]);
                i++;
        }

       // getchar();why you were using this?

        fclose(file_in);
      //  return 0;
}

void write(char str[])
{
        FILE *file_in;
        file_in=fopen("test.txt","a");

        if (file_in == NULL) {
                printf("Error opening file\n");
        }

        //write to the file
        fprintf(file_in, "\n%s", str);
//      fputs(str, file_in);

        fclose(file_in);
}

int main()
{
        char msg[50];
        read();



        printf("Enter some text: ");
       // getchar();
        gets(msg);//It reads in msg 

        write(msg); 

        return 0;
}

4 Comments

Your welcome..I did not provide the changes. You should go through others answers and donot use gets use fgets instead.
The down voter of this answer should really leave a reason. i.e. poster is fairly new to SO, give him an idea of how to improve the answer.
@jahan - regarding your comment // getchar();why you were using this?. In a console app, getchar() is a good way to pause execution so you can see the output of a printf() before execution collapses the console.
@ryyker thanks for telling me that. You know some people are addicted to down vote here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.