C read in string instead file [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this
int main(int argc, char* argv[])
{
FILE *file = fopen("text", "rb");
char line[128];
while(fgets(line, sizeof(line), file))
{
printf("%s", line);
}
return 0;
}
I would like to remove the load from the file.
This is part of the program.
I want to leave while fgets
I try this
char file[] = "name = xxxx\nsurname = xxx\n adress = xxx";

If I understand your question, and you're on a system that supports it, fmemopen() might do what you want.

Related

putting '0' before ID number that the user input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm a new c programmer.
I was asked to get an ID number as an input and if the ID length < 9 I need to put '0' before the number.
example:
input: 12345
output: 000012345
I can use the libraries: stdio, stdlib, string, math and time.
You can use printf for this:
#include <stdio.h>
int main () {
printf("%09d\n", 12345);
return(0);
}
Documentation
There is an easy solution in here.
But if you want a hard solution, where you juggle around with strings and such, you can do the following:
convert your id into a string (if it isn't already one, but argv should a string by default)
calculate the number of zeros you need, by doing 9-idString Length
create a "buffer" string variable
for loop from 0 to 9-idString and use strcat to add "0"s to your buffer string.
add your id to your buffer string using strcat
Example code would be something like this:
int id = 12345;
char idstring[10];
char buffer[10];
sprintf(idstring, "%d", id);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
argv variant:
char idstring[10];
char buffer[10];
sprintf(idstring, "%s", argv[1]);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);

List of "Always wrong" snippets in C [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Was talking to a colleague today on one-spot errors - I.e. errors (or at least patterns that should ring an alarm bell) in code that a decent programmer should be able to spot at a single glance like
x = malloc (strlen(y));
while (!feof (f)) {
...
}
char *f(){
char x[100];
...
return x;
}
Who has similar snippets of such patterns? I would suggest anyone who has been on SO for a while will have his personal favourites of those
char *buf;
scanf("%s", buf);
This is wrong, because no memory has been allocated for buf.
char buf[100];
scanf("%s", &buf);
This is wrong, because scanf expects a char *, not a char (*)[n].
char c;
while ((c = getchar()) != EOF)
putchar(c);
This is wrong, because EOF does not fit in the range of a char. Use int instead.
fflush(stdin);
fflush is undefined for input streams, like stdin, albeit this is implemented as an extension in some compilers, like Microsoft C.
#define IN 0;
Do not put semicolons at the end of a #define.
blk = realloc(blk, n);
If realloc fails, any contents in blk will be lost, because realloc will return NULL. To solve the problem, copy the return value into a temporary and only if the temporary is not NULL, copy to the final destination.

C, read by line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm making a RPG game in C, and I need to read from a single file. Having multiple files is very tedious. Like "health.dat", "energy.dat", "money.dat" etc. I want to put them into one "data.dat", is this possible?
I just want to read specific line from a file
If line 1 has "number 1", then it should go to the "a" variable, line 2 should go to the "b" variable, etc.
Many thanks.
It is possible.
you can use the array of pointers in the fgets function.
char *fgets(char *s, int size + 1, FILE *stream);
fgets (array,size + 1,stream);
where array is *array[size];
You can achieve what you describe using the fgets function as follows:
#include <stdio.h>
#include <stdlib.h>
const int MAX_LENGTH = 256;
int main (void)
{
// Open the file
FILE* file = fopen ("data.dat", "r");
// Read the lines
char a[MAX_LENGTH];
fgets (a, MAX_LENGTH - 1, file);
char b[MAX_LENGTH];
fgets (b, MAX_LENGTH - 1, file);
// ...
// Close the file
fclose (file);
return EXIT_SUCCESS;
}
Set the MAX_LENGTH variable to the maximum expected length of a line in your file. You should also test fopen and fgets for returning NULL, as both are error conditions.

Parse int and char into two separate values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having the issues of trying to separate 1s or 2d into int 1 and char 's' and then storing them as separate values. How would I go about doing this. I know I can't do it with strtok since there are no delimiters.
Use strtol. For example,
char *endptr;
val = strtol(input_str, &endptr, 10;
next_char = *endptr;
As discussed in the manpage, http://man7.org/linux/man-pages/man3/strtol.3.html, the second parameter is a pointer to a char pointer and after the conversion, the char pointer points to the following character.
You can access each char from the string by index. To convert char to int (not by ascii value, '1' to 1) you just do the following:
int a = c[0]-'0';
and for the char:
char b = c[1];

Print header defined size string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to print in a file a string with a fixed size. Something like this:
#define SIZE 30
main()
{
FILE *fp = fopen("myfile.txt","w+");
char s[10];
sprintf(s, "my text");
fprintf(fp, "%SIZEs", s);
fclose(fp);
}
but I keep getting errors.. help?
You should define your format string like the following:
fprintf(fp, "%*s", SIZE, s); // Right aligned string
fprintf(fp, "%-*s", SIZE, s); // Left aligned string
From the printf man page:
The precision
Instead of a decimal digit string one may write "*" to specify that the precision is given in the next argument

Resources