passing a string into a function

user3614562

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?

jahan

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing string parameter in JavaScript function

From Dev

Passing a string to function but expecting an array

From Dev

Passing a string to pthread function

From Dev

Passing a String Array to a Function

From Dev

Trouble with passing string to function

From Dev

Passing String to Import Function React

From Dev

Passing string from a function into another function

From Dev

Passing HTML string to JavaScript function

From Dev

passing string to ggplot function

From Dev

passing string to a function

From Dev

Passing string parameters in JQuery function

From Dev

Passing encoded string to Javascript function

From Dev

Passing a string as a keyed argument to a function

From Dev

Passing a pointer (string) to a C function

From Dev

alert by passing string to alert function

From Dev

Passing default string pointer to function

From Dev

String vectors not passing to a class function

From Dev

Passing spaced string as argument to function

From Dev

Passing null string to function as an argument

From Dev

C passing string to function issue

From Dev

Passing path string to a jquery function

From Dev

Passing swift string to c function char *

From Dev

Passing string into a lambda function

From Dev

Passing a string array to another function

From Dev

Passing input string to a function in C

From Dev

Passing a string to a handler function in Go

From Dev

passing function statement as string in terraform

From Dev

passing a string variable to a function np.array

From Dev

Passing string in an onclick function